forked from swharden/Spectrogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColormap.cs
More file actions
95 lines (81 loc) · 3.14 KB
/
Colormap.cs
File metadata and controls
95 lines (81 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Drawing;
using System.Linq;
namespace Spectrogram
{
public class Colormap
{
public static Colormap Argo => new Colormap(new Colormaps.Argo());
public static Colormap Blues => new Colormap(new Colormaps.Blues());
public static Colormap Grayscale => new Colormap(new Colormaps.Grayscale());
public static Colormap GrayscaleReversed => new Colormap(new Colormaps.GrayscaleR());
public static Colormap Greens => new Colormap(new Colormaps.Greens());
public static Colormap Inferno => new Colormap(new Colormaps.Inferno());
public static Colormap Lopora => new Colormap(new Colormaps.Lopora());
public static Colormap Magma => new Colormap(new Colormaps.Magma());
public static Colormap Plasma => new Colormap(new Colormaps.Plasma());
public static Colormap Turbo => new Colormap(new Colormaps.Turbo());
public static Colormap Viridis => new Colormap(new Colormaps.Viridis());
private readonly IColormap cmap;
public readonly string Name;
public Colormap(IColormap colormap)
{
cmap = colormap ?? new Colormaps.Grayscale();
Name = cmap.GetType().Name;
}
public override string ToString()
{
return $"Colormap {Name}";
}
public static Colormap[] GetColormaps() =>
typeof(Colormap).GetProperties()
.Select(x => (Colormap)x.GetValue(x.Name))
.ToArray();
public static string[] GetColormapNames()
{
return GetColormaps().Select(x => x.Name).ToArray();
}
public static Colormap GetColormap(string colormapName)
{
foreach (Colormap cmap in GetColormaps())
if (string.Equals(cmap.Name, colormapName, StringComparison.InvariantCultureIgnoreCase))
return cmap;
throw new ArgumentException($"Colormap does not exist: {colormapName}");
}
public (byte r, byte g, byte b) GetRGB(byte value)
{
return cmap.GetRGB(value);
}
public (byte r, byte g, byte b) GetRGB(double fraction)
{
fraction = Math.Max(fraction, 0);
fraction = Math.Min(fraction, 1);
return cmap.GetRGB((byte)(fraction * 255));
}
public int GetInt32(byte value)
{
var (r, g, b) = GetRGB(value);
return 255 << 24 | r << 16 | g << 8 | b;
}
public int GetInt32(double fraction)
{
var (r, g, b) = GetRGB(fraction);
return 255 << 24 | r << 16 | g << 8 | b;
}
public Color GetColor(byte value)
{
return Color.FromArgb(GetInt32(value));
}
public Color GetColor(double fraction)
{
return Color.FromArgb(GetInt32(fraction));
}
public void Apply(Bitmap bmp)
{
System.Drawing.Imaging.ColorPalette pal = bmp.Palette;
for (int i = 0; i < 256; i++)
pal.Entries[i] = GetColor((byte)i);
bmp.Palette = pal;
}
}
}