Skip to content

Instantly share code, notes, and snippets.

@Wendelstein7
Last active April 16, 2020 18:04
Show Gist options
  • Save Wendelstein7/f205f0dd94ebb1849eb05c842e249f3d to your computer and use it in GitHub Desktop.
Save Wendelstein7/f205f0dd94ebb1849eb05c842e249f3d to your computer and use it in GitHub Desktop.
Generating an image to use as a hue value cheat sheet (hue in degrees, factor, bytes and hexadecimal)
// HSV to RGB code from http://www.splinter.com.au/converting-hsv-to-rgb-colour-using-c/
// Other code by me
#r "System.Drawing";
using System.Drawing;
void HsvToRgb(double h, double S, double V, out int r, out int g, out int b)
{
// T. Nathan Mundhenk mundhenk@usc.edu
double H = h;
while (H < 0) { H += 360; };
while (H >= 360) { H -= 360; };
double R, G, B;
if (V <= 0)
{ R = G = B = 0; }
else if (S <= 0)
{
R = G = B = V;
}
else
{
double hf = H / 60.0;
int i = (int)Math.Floor(hf);
double f = hf - i;
double pv = V * (1 - S);
double qv = V * (1 - S * f);
double tv = V * (1 - S * (1 - f));
switch (i)
{
// Red is the dominant color
case 0:
R = V;
G = tv;
B = pv;
break;
// Green is the dominant color
case 1:
R = qv;
G = V;
B = pv;
break;
case 2:
R = pv;
G = V;
B = tv;
break;
// Blue is the dominant color
case 3:
R = pv;
G = qv;
B = V;
break;
case 4:
R = tv;
G = pv;
B = V;
break;
// Red is the dominant color
case 5:
R = V;
G = pv;
B = qv;
break;
// Just in case we overshoot on our math by a little, we put these here. Since its a switch it won't slow us down at all to put these here.
case 6:
R = V;
G = tv;
B = pv;
break;
case -1:
R = V;
G = pv;
B = qv;
break;
// The color is not defined, we should throw an error.
default:
//LFATAL("i Value error in Pixel conversion, Value is %d", i);
R = G = B = V; // Just pretend its black/white
break;
}
}
r = Clamp((int)(R * 255.0));
g = Clamp((int)(G * 255.0));
b = Clamp((int)(B * 255.0));
}
int Clamp(int i)
{
if (i < 0) return 0;
if (i > 255) return 255;
return i;
}
Bitmap bitmap = new Bitmap(1024, 128);
for (int x = 0; x < 1024; x++)
{
int r = 0;
int g = 0;
int b = 0;
HsvToRgb((x / 1024D) * 360D, 1, 1, out r, out g, out b);
for (int y = 0; y < 128; y++)
{
bitmap.SetPixel(x, y, Color.FromArgb(byte.MaxValue, r, g, b));
}
}
Graphics graphics = Graphics.FromImage(bitmap);
for (int i = 0; i < 1024; i += 24)
{
graphics.DrawLine(p, i, 0, i, 32);
graphics.DrawLine(p, i, 96, i, 127);
graphics.DrawString((i / 1024D).ToString("0.00"), new Font("Lucida Console", 5), Brushes.Black, i - 8, 48 - 9);
graphics.DrawString((i / (1024D / 360D)).ToString("000") + "°", new Font("Lucida Console", 5), Brushes.Black, i - 8, 64 - 9);
graphics.DrawString((i / 4).ToString("000"), new Font("Lucida Console", 5), Brushes.Black, i - 8, 80 - 9);
graphics.DrawString("0x" + (i / 4).ToString("X2").ToUpper(), new Font("Lucida Console", 5), Brushes.Black, i - 8, 96 - 9);
}
bitmap.Save("huemap.png", ImageFormat.Png);
// look for huemap.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment