Color spectrum

Hello everyone. I hope that all is fine for you. I am still playing with pixels. Now I am searching for a way to display a cool color spectrum using a simple loop which starts at coordinate 0, 0 and goes until screenWidth & screenHeight values. Do you have an idea how I could draw something more or less similar with this spectrum ?

https://www.colortools.net/color_spectrum.html

How should I compute r, g and b ?
1
2
3
4
5
6
7
8
9
10
11
for (int x = 0; x < ScreenWidth(); x++)
{
    for (int y = 0; y < ScreenHeight(); y++)
    {			
	r = uint8_t for red
	g = uint8_t for green
	b = uint8_t for blue

	Draw(gck::vi2d(x, y), gck::Pixel(r, g, b));
   }
}

Last edited on
its better to draw that using HSL instead of RGB. (you can write a simple function to convert between these).
Then its a simple iteration like you have above. With RGB, its less simple (you can still do it, but its not a simple incrementing pattern).
Last edited on
Thank you for your help Jonnin. I took a look at your explanation about HSL and finally I have added this function which works well as expected (but HSV), displaying a full color spectrum like in the previous picture. I share it with you. It can be useful ++

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
gck::Pixel HSVtoRGB(float H, float S, float V) 
{
	float s = S / 100;
	float v = V / 100;
	float C = s * v;
	float X = C * (1 - fabs(fmod(H / 60.0, 2) - 1));
	float m = v - C;
	float r, g, b;

	if (H >= 0 && H < 60)
		r = C, g = X, b = 0;
	else if (H >= 60 && H < 120)
		r = X, g = C, b = 0;
	else if (H >= 120 && H < 180)
		r = 0, g = C, b = X;
	else if (H >= 180 && H < 240)
		r = 0, g = X, b = C;
	else if (H >= 240 && H < 300)
		r = X, g = 0, b = C;
	else
		r = C, g = 0, b = X;

	int R = (r + m) * 255;
	int G = (g + m) * 255;
	int B = (b + m) * 255;

	return gck::Pixel(R, G, B);
}


So in my project, I display each pixel this way :
1
2
3
4
5
6
7
8
9
for (int x = 0; x < ScreenWidth(); x++)
	for (int y = 0; y < ScreenHeight(); y++)
	{
		float hue = (float(x) / float(ScreenWidth())) * 360;
		float saturation = (float(y) / float(ScreenHeight()) * 100);
		float lightness = 100; // or another value in case of HSV

		Draw(gck::vi2d(x, y), HSVtoRGB(hue, saturation, lightness));
	}
Last edited on
Done. Thank you ++

https://ibb.co/VDt23qs
Topic archived. No new replies allowed.