forked from swharden/Spectrogram
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColormap.cs
More file actions
91 lines (72 loc) · 2.48 KB
/
Colormap.cs
File metadata and controls
91 lines (72 loc) · 2.48 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
using System;
using System.Linq;
using SkiaSharp;
namespace Spectrogram;
public class Colormap(ScottPlot.IColormap colormap)
{
private ScottPlot.IColormap _Colormap { get; } = colormap;
public string Name => _Colormap.Name;
public override string ToString() => _Colormap.ToString();
public static Colormap[] GetColormaps() => ScottPlot.Colormap.GetColormaps()
.Select(x => new Colormap(x))
.ToArray();
public static string[] GetColormapNames() => ScottPlot.Colormap.GetColormaps()
.Select(x => new Colormap(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) => GetRGB(value / 255.0);
public (byte r, byte g, byte b) GetRGB(double fraction)
{
ScottPlot.Color color = _Colormap.GetColor(fraction);
return (color.R, color.G, color.B);
}
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 SKColor GetColor(byte value)
{
var color = GetInt32(value);
return new SKColor((uint)color);
}
public SKColor GetColor(double fraction)
{
var color = GetInt32(fraction);
return new SKColor((uint)color);
}
public SKBitmap ApplyFilter(SKBitmap bmp)
{
SKImageInfo info = new(bmp.Width, bmp.Height, SKColorType.Rgba8888);
SKBitmap newBitmap = new(info);
using SKCanvas canvas = new(newBitmap);
canvas.Clear();
using SKPaint paint = new SKPaint();
byte[] A = new byte[256];
byte[] R = new byte[256];
byte[] G = new byte[256];
byte[] B = new byte[256];
for (int i = 0; i < 256; i++)
{
var color = GetColor((byte)i);
A[i] = color.Alpha;
R[i] = color.Red;
G[i] = color.Green;
B[i] = color.Blue;
}
paint.ColorFilter = SKColorFilter.CreateTable(A, R, G, B);
canvas.DrawBitmap(bmp, 0, 0, paint);
return newBitmap;
}
}