Skip to content

Commit 2348a44

Browse files
committed
improve colormap abstraction
1 parent cbd9119 commit 2348a44

12 files changed

Lines changed: 989 additions & 864 deletions

File tree

src/AudioMonitor/Form1.Designer.cs

Lines changed: 35 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AudioMonitor/Form1.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public Form1()
2626
cbDisplay.Items.Add("horizontal repeat");
2727
cbDisplay.Items.Add("waterfall");
2828
cbDisplay.SelectedItem = cbDisplay.Items[0];
29+
30+
string[] colormapNames = Enum.GetNames(typeof(Spectrogram.Colormap));
31+
Array.Sort(colormapNames);
32+
cbColormap.Items.AddRange(colormapNames);
33+
cbColormap.SelectedItem = cbColormap.Items[cbColormap.Items.Count - 1];
2934
}
3035

3136
private void Form1_Load(object sender, EventArgs e)
@@ -101,12 +106,15 @@ private void Timer1_Tick(object sender, EventArgs e)
101106
else
102107
busyRendering = true;
103108

109+
Spectrogram.Colormap colormap = (Spectrogram.Colormap)Enum.Parse(typeof(Spectrogram.Colormap), cbColormap.Text);
110+
104111
pictureBox1.BackgroundImage = spec.GetBitmap(
105112
intensity: (float)nudIntensity.Value,
106113
decibels: cbDecibels.Checked,
107114
frequencyMin: 0,
108115
frequencyMax: 4000,
109-
vertical: waterfall
116+
vertical: waterfall,
117+
colormap: colormap
110118
);
111119
lblStatus.Text = $"spectrogram contains {spec.fftList.Count} FFT samples | last render: {spec.GetLastRenderTime()} ms";
112120
renderNeeded = false;

src/Spectrogram/Colormap.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Text;
5+
6+
namespace Spectrogram
7+
{
8+
public enum Colormap { grayscale, viridis, vdGreen, vdBlue }
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Text;
5+
6+
namespace Spectrogram.Colormaps
7+
{
8+
public abstract class Colormap
9+
{
10+
public abstract string GetName();
11+
public abstract void Apply(Bitmap bmp);
12+
}
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Drawing.Imaging;
5+
using System.Text;
6+
7+
namespace Spectrogram.Colormaps
8+
{
9+
class Grayscale : Colormap
10+
{
11+
public override string GetName()
12+
{
13+
return "Grayscale";
14+
}
15+
16+
public override void Apply(Bitmap bmp)
17+
{
18+
ColorPalette pal = bmp.Palette;
19+
20+
for (int i=0; i<256; i++)
21+
pal.Entries[i] = Color.FromArgb(255, i, i, i);
22+
23+
bmp.Palette = pal;
24+
}
25+
26+
}
27+
}

src/Spectrogram/Colormaps/Jet.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Drawing.Imaging;
5+
using System.Text;
6+
7+
namespace Spectrogram.Colormaps
8+
{
9+
class Jet : Colormap
10+
{
11+
public override string GetName()
12+
{
13+
return "Grayscale";
14+
}
15+
16+
public override void Apply(Bitmap bmp)
17+
{
18+
ColorPalette pal = bmp.Palette;
19+
20+
bmp.Palette = pal;
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)