{"id":19559815,"url":"https://github.com/ar1st0crat/scicolormaps","last_synced_at":"2025-04-26T23:32:43.232Z","repository":{"id":93763194,"uuid":"86254310","full_name":"ar1st0crat/SciColorMaps","owner":"ar1st0crat","description":"Custom .NET color maps (user-defined or imported from matplotlib) for scientific visualization","archived":false,"fork":false,"pushed_at":"2020-05-05T10:44:08.000Z","size":2305,"stargazers_count":39,"open_issues_count":1,"forks_count":6,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-25T18:18:43.571Z","etag":null,"topics":["colormap","matplotlib","nuget","uwp","visualization","winforms","wpf"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ar1st0crat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-26T18:16:21.000Z","updated_at":"2024-10-21T15:03:15.000Z","dependencies_parsed_at":"2023-03-05T10:00:27.052Z","dependency_job_id":null,"html_url":"https://github.com/ar1st0crat/SciColorMaps","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ar1st0crat%2FSciColorMaps","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ar1st0crat%2FSciColorMaps/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ar1st0crat%2FSciColorMaps/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ar1st0crat%2FSciColorMaps/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ar1st0crat","download_url":"https://codeload.github.com/ar1st0crat/SciColorMaps/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251068040,"owners_count":21531475,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["colormap","matplotlib","nuget","uwp","visualization","winforms","wpf"],"created_at":"2024-11-11T05:04:16.624Z","updated_at":"2025-04-26T23:32:42.180Z","avatar_url":"https://github.com/ar1st0crat.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SciColorMaps\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n![NuGet](https://img.shields.io/nuget/dt/ar1st0crat.SciColorMaps.Portable)\n\nSimple and convenient library providing custom .NET color maps (user-defined or imported from matplotlib) for scientific visualization.\n\nLibrary comes in two versions (also available as NuGet packages):\n\n- **SciColorMaps.Portable.dll** does not rely on any framework-dependent color structure (like ```System.Drawing.Color``` or ```System.Windows.Media.Color```). It operates with pure byte arrays, so it can be used in any .NET project.\n\n- **SciColorMaps.dll** provides some additional functions that handle GDI+ colors, so it can be used in WinForms and WPF projects.\n\n*NuGet packages contain binaries with all 75 matplotlib colormaps included. In most cases only a small subset of them is what is really needed. Luckily, SciColorMaps can be customized and recompiled quite easily. This repo includes special helper script for making your own SciColorMaps dll. Please [read instructions here](#compiling-scicolormaps) how to use it.*\n\n\n## Base ColorMap class\n\nUsage example:\n\n```C#\n// create 'viridis' colormap with 256 colors, mapping values from range [0, 1]\nvar cmap1 = new ColorMap();\n\n// create 'rainbow' colormap with 256 colors, mapping values from range [0, 1]\nvar cmap2 = new ColorMap(\"rainbow\");\n\n// create 'gnuplot' colormap with 256 colors, mapping values from range [-7.5, 7.5]\nvar cmap3 = new ColorMap(\"gnuplot\", -7.5, 7.5);\n\n// create 'coolwarm' colormap with 64 colors, mapping values from range [10, 100]\nvar cmap = new ColorMap(\"coolwarm\", 10, 100, 64);\n\n// get color corresponding to value 25\nvar color = cmap[25];\n\n// print names of all available predefined palettes\nforeach (var palette in ColorMap.Palettes)\n{\n    Console.WriteLine(palette);\n}\n\n// prints full set of 75 matplotlib colormaps:\n// accent\n// afmhot\n// autumn\n// ...\n// ylorbr\n// ylorrd\n\n```\n\n![pic1](https://github.com/ar1st0crat/SciColorMaps/blob/master/Screenshots/WinForms.png)\n\n![pic2](https://github.com/ar1st0crat/SciColorMaps/blob/master/Screenshots/WinForms2.png)\n\n\n## User-defined colormaps\n\nUsers can create their own color palettes, like this:\n\n```C#\n// === works for both versions of SciColorMaps ===\n\nvar rgbs = new [] { \n                    new byte[] {0, 0, 0},\n                    new byte[] {192, 0, 0},\n                    new byte[] {255, 224, 255}\n                  };\nvar positions = new float[] { 0, 0.4f, 1 };\n\n// 1) static factory method with default colormap parameters:\nvar cmap1 = ColorMap.CreateFromColors(rgbs, positions);\n\n// 2) static factory method, full set of parameters:\nvar cmap2 = ColorMap.CreateFromColors(rgbs, positions, 10, 100, 32);\n\n// 3) ColorMap constructor:\nvar cmap3 = new ColorMap(\"my_own_colormap\", -2, 2, 128, rgbs, positions);\n\n\n// === won't compile for SciColorMaps.Portable ===\n\nvar colors = new Color[] { Color.Black, Color.Blue, Color.White };\n\n// 4) static factory method with default colormap parameters:\nvar cmap4 = ColorMap.CreateFromColors(colors, positions);\n\n// 5) static factory method, full set of parameters:\nvar cmap5 = ColorMap.CreateFromColors(colors, positions, 10, 100, 32);\n\n// 6) ColorMap constructor:\nvar cmap6 = new ColorMap(\"fancy_colormap\", 10, 100, 32, colors, positions);\n\n```\n\nOptions 3 and 6 allow user setting the name of the custom colormap. Otherwise the name is set by default: \"user\".\n\nNote also, the first color position should be 0.0f and last position should be 1.0f (otherwise an ArgumentException will be thrown).\n\n*Byte arrays can be easily converted to any framework-specific color structures/classes.*\n\nFor example, in WPF the following extension method can be used:\n\n```C#\nusing SciColorMaps.Portable;\n\npublic static class ColorUtils\n{\n    public static Color ToMediaColor(this byte[] rgb)\n    {\n        return Color.FromRgb(rgb[0], rgb[1], rgb[2]);\n    }\n}\n\n...\n\nvar cmap = new ColorMap(\"ocean\");\nvar color = cmap[0.3].ToMediaColor();\n\n```\n\n![User-defined](https://github.com/ar1st0crat/SciColorMaps/blob/master/Screenshots/WinFormsCustom.png)\n\n\n## Grayscale colormaps\n\nSciColorMaps provides the ```GrayColorMap``` decorator class. This class replaces base palette with its grayscale analog during construction.\n\nUsage example:\n\n```C#\n// upper sreenshot\nvar cmap1 = new GrayColorMap(new ColorMap(\"gnuplot2\", min, max));\n\n// lower screenshot\nvar cmap2 = new GrayColorMap(new ColorMap(\"gnuplot2\", min, max), GrayScaleOptions.Lightness);\n\n\n// the three simple algorithms for conversion are implemented\n// (as in GIMP: https://docs.gimp.org/2.6/en/gimp-tool-desaturate.html)\n\n// public enum GrayScaleOptions\n// {\n//     Luminosity,      // gray = 0.21*R + 0.72*G + 0.07*B\n//     Lightness,       // gray = (max(R, G, B) + min(R, G, B)) / 2\n//     Average          // gray = (R + G + B) / 3\n// }\n\n// GrayScaleOptions.Luminosity is used by default\n\n```\n\n![Grayscale](https://github.com/ar1st0crat/SciColorMaps/blob/master/Screenshots/WinFormsGray.png)\n\n\n## Mirrored colormaps\n\nSciColorMaps also provides the ```MirrorColorMap``` decorator class. This class replaces base palette with its mirrored analog during construction.\n\nUsage example:\n\n```C#\n// mirrored colors\nvar cmap = new MirrorColorMap(new ColorMap(\"ocean\"));\n\n```\n\n\n## WinForms demo app\n\nLeft button click on surface 2d area -\u003e change colormap to its mirrored version.\n\nRight button click on surface 2d area -\u003e change colormap to its grayscale version.\n\nLeft button click on colormap strip -\u003e open dialog for constructing your own palette.\n\n\n## WPF demo app\n\nShows how to use SciColorMaps.Portable.\n\n![WPF](https://github.com/ar1st0crat/SciColorMaps/blob/master/Screenshots/Wpf.png)\n\n\n## UWP demo app\n\nShows how to use SciColorMaps.Portable in UWP projects.\n\nThe app allows loading sound from wav/mp3/mp4 files as well as recording sound from any input device (most likely, the microphone). When the audio signal is loaded, the app evaluates and visualizes its spectrogram in 2D and 3D. The visualization is based on the colormap selected in the left panel of a main page. Check it out: 3D-spectrogram with 'blues' colormap looks just like mountains in the fog ))\n\n![UWP](https://github.com/ar1st0crat/SciColorMaps/blob/master/Screenshots/Uwp.png)\n\n\n## Compiling SciColorMaps\n\nAll customizable parameters are contained in ```Palette.cs``` file. So there's a helper script that can generate this file according to particular needs. Here's how to use it:\n\n1) Download or clone this repo\n\n2) Go to ```matplotlib``` folder and edit ```colormaps.txt``` text file, e.g.:\n\n```\nocean\nhot\ncoolwarm\n```\n\nBasically you include here only colormaps you really need. You can view all available colormap names in ```colormaps``` subfolder. If you specify non-existing colormap it'll be simply ignored.\n\nNote also that 'viridis' will be included to the list anyway since it's the default modern colormap.\n\n3) run ```generate_cs.py``` script:\n\n```\npython generate_cs.py [-r 16] [-p] [-a]\n```\n\n```-r``` - colormap resolution (number of base colors; should be 8, 16, 32, 64, 128 or 256)\n\n```-p``` - if specified then generate cs file for portable SciColorMaps version\n\n```-a``` - if specified then generate cs file with all palettes found in ```colormaps``` folder (ignoring ```colormaps.txt``` input file)\n\nExamples:\n```\n// generate cs file with all palettes found in colormaps folder \n// (ignore colormaps.txt) and resolution = 8 base colors\n\npython generate_cs.py -r 8 -a\n\n\n// generate cs file for portable version \n// (with default resolution = 16 base colors)\n\npython generate_cs.py -p\n```\n\n4) go to ```cs``` subfolder and copy generated ```Palette.cs``` file to ```SciColorMaps``` or ```SciColorMaps.Portable``` project folder.\n\n5) open ```SciColorMaps.sln``` in Visual Studio and compile new dll.\n\n\n**RECTANGULAR conditional compilation symbol**\n\nAccessing elements in jagged arrays is significantly faster (up to **40%**) compared to rectangular arrays, hence the jagged arrays are used and compiled by default. If an efficient memory management is of more importance then compile SciColorMaps with the 'RECTANGULAR' conditional compilation symbol.\n\n| compilation   | dll size |\n----------------|-----------\n| default       | ~95kb    |\n| RECTANGULAR   | ~29kb    |\n\nAlso, if you are worried about the memory usage, please note that palette arrays are instantiated *lazily* in the calling code, e.g.:\n\n```C#\nvar palette = Palette.GnuPlot.Value;\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Far1st0crat%2Fscicolormaps","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Far1st0crat%2Fscicolormaps","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Far1st0crat%2Fscicolormaps/lists"}