Skip to content

Commit 21f588e

Browse files
committed
support getting colormaps by their name
#18
1 parent 8f42496 commit 21f588e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Spectrogram.Tests/ColormapExamples.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,15 @@ public void Test_Make_CommonColormaps()
3232
Debug.WriteLine($"![](dev/graphics/hal-{cmap.Name}.png)");
3333
}
3434
}
35+
36+
[Test]
37+
public void Test_Colormaps_ByName()
38+
{
39+
string[] names = Colormap.GetColormapNames();
40+
Console.WriteLine(string.Join(", ", names));
41+
42+
Colormap viridisCmap = Colormap.GetColormap("viridis");
43+
Assert.AreEqual("Viridis", viridisCmap.Name);
44+
}
3545
}
3646
}

src/Spectrogram/Colormap.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using System.Drawing;
45
using System.Linq;
56
using System.Text;
@@ -46,6 +47,20 @@ public static Colormap[] GetColormaps()
4647
return ics.Select(x => new Colormap(x)).ToArray();
4748
}
4849

50+
public static string[] GetColormapNames()
51+
{
52+
return GetColormaps().Select(x => x.Name).ToArray();
53+
}
54+
55+
public static Colormap GetColormap(string colormapName)
56+
{
57+
foreach (Colormap cmap in GetColormaps())
58+
if (string.Equals(cmap.Name, colormapName, StringComparison.InvariantCultureIgnoreCase))
59+
return cmap;
60+
61+
throw new ArgumentException($"Colormap does not exist: {colormapName}");
62+
}
63+
4964
public (byte r, byte g, byte b) GetRGB(byte value)
5065
{
5166
return cmap.GetRGB(value);

0 commit comments

Comments
 (0)