Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » color picker

Credits go to gnolam and Johan Halmén for helping out!
This thread is locked; no one can reply to it. rss feed Print
color picker
karistouf
Member #5,126
October 2004
avatar

hum... hi all!
i m searching for examples/lib to do the Gimp Color Wheel type inside my allegro/OL app.
Sure im to much weak in maths, so any help will be vry appreciated !
Any idea on how to draw the circular color wheel and draw the triangle, with H S V values ?

{"name":"Triangulo_HSV.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/d\/3dea04cb277c3c5280a768e8b9cb7c23.png","w":347,"h":347,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/d\/3dea04cb277c3c5280a768e8b9cb7c23"}Triangulo_HSV.png

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Drawing the hue circle should be easy :
Draw the radius of the outer edge of the circle in a fully saturated, fully valued hue over 360 degrees. Draw a filled circle with a smaller radius over the top using the bitmap mask color. This one can easily be a pre-drawn overlay.

The saturation and value triangle will be more difficult and you will probably have to redraw it each time the hue changes unless you limit the different number of hues allowed. For example 360 different pre-drawn saturation value triangles might not be too much of a strain on memory, but it depends on how large they are.

The triangle itself is basically composed of a set of gradient line segments that get shorter and shorter as you move down and left in the triangle. I would write my own gradient line function for it. Pass the function the hue and the value of the current line and inside the function vary the saturation from 0.0 at one end gradually up to 1.0 at the other end of the line segment. Repeat this for each line the triangle is composed of, varying the color value as you move down and left.

Johan Halmén
Member #1,550
September 2001

Isn't there some Gouraud routines for drawing triangles in Allegro? You set the colour of each vertex of the triangle. Black, white, full saturated hue. Then let Allegro draw it.

[edit]
Apparently not. There's a non-rotated gouraud rectangle thing.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

karistouf
Member #5,126
October 2004
avatar

Quote:

Drawing the hue circle should be easy

completely agriing Edgardo. But for the triangle I have NO idea about how to proceed. neither how to use a vector to print to screen the result of calculation in pixels.

does anybody knows where the libs that were on allegro once gone ? : altheme / http://www.allegro.cc/go.php?http://www.the-good-stuff.freeserve.co.uk/allegro/gui/mage/ ?

Don Freeman
Member #5,110
October 2004
avatar

Gimp is open source, so you COULD take a look at the source code to see how they do it.::) I don't really have a clue off hand how to do the triangle...the circle would be relatively easy.

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

TheFayl
Member #8,641
May 2007
avatar

Quote:

You set the colour of each vertex of the triangle.

For sure OpenGL can do it.

glBegin(GLTRIANGLES);
glColor(somecolor);
glVertex(coords);
glColor(somecolor);
glVertex(coords);
glColor(somecolor);
glVertex(coords);
glEnd();

Well, or something like this. :)

gnolam
Member #2,030
March 2002
avatar

Johan Halmén said:

Isn't there some Gouraud routines for drawing triangles in Allegro? You set the colour of each vertex of the triangle. Black, white, full saturated hue. Then let Allegro draw it.

[edit]
Apparently not. There's a non-rotated gouraud rectangle thing.

Yes there is.void triangle3d_f(BITMAP *bmp, int type, BITMAP *tex, V3D_f *v1, *v2, *v3);With POLYTYPE_GCOL as the type. You'll have to check for yourself if it can be used as-is for a WYSIWYG color picker though.

As for the rest of the implementation: I assume you're familiar with barycentric coordinates? If not, read up on them now...

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Johan Halmén
Member #1,550
September 2001

I'd do the triangle like this:
1. Calculate the coordinates of the three vertices.
2. Think of the triangle as a set of horizontal lines.
2.1. Calculate start and end point of each line.
2.2. Calculate the colour of the start point and end point by interpolating between the two vertices in concern.
2.3. Draw each pixel of the horizontal line by interpolating between the start and end point.

Another way would be to draw the gouraud rectangle and mask it to a triangle and rotate_blit it.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

karistouf
Member #5,126
October 2004
avatar

thxs... i keep you in touch about this....

SiegeLord
Member #7,827
October 2006
avatar

I'm a little confused why you are ignoring gnolam's suggestion... but I guess if you are willing to wait, I'll have a gouraud shaded triangle ready in a day or two for A5, and I can give it to you.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Johan Halmén
Member #1,550
September 2001

Yes, don't use my re-invented wheel. Do what Gnolam said. This I did in 4 minutes.
{"name":"596636","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/6\/767fbfead20af16f20e2dc1650870c7b.png","w":336,"h":331,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/6\/767fbfead20af16f20e2dc1650870c7b"}596636

The actual colour picking I would do with getpixel from that triangle. It won't contain all possible values in RGB space, at least the Gimp colour picker is so small that it skips over a lot of values stepping from one pixel to the next. But that can be adjusted with the sliders, if you have a slider for each channel.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Evert
Member #794
November 2000
avatar

Assuming you drew it correctly, that triangle is basically a wedge out the (V, S) plane, tilted at an odd angle.
Drawing the plane should be easy, so too is removing the wedge. I'm guessing that you have to remove the 45 degree edge. In the picture, the triangle is equilateral, which you can get by stretching one of the axes in the plane by sqrt(2). All you then have to do is rotate the triangle, which shouldn't cause you too much of a headache.

Johan Halmén
Member #1,550
September 2001

My code:

1 set_color_depth(32);
2 if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0)) {
3 allegro_message("Error setting 320x200x8 gfx mode:\n%s\n", allegro_error);
4 return -1;
5 }
6 V3D_f v1 =
7 {
8 200., 50., 0,
9 0., 0.,
10 makecol(0, 0, 0) // black vertex
11 };
12 V3D_f v2 =
13 {
14 150., 350., 0,
15 0., 0.,
16 makecol(255, 255, 255) // white vertex
17 };
18 V3D_f v3 =
19 {
20 400., 150., 0,
21 0., 0.,
22 makecol(255, 0, 0) // red vertex
23 };
24
25 clear_to_color(screen, makecol(128, 128, 128));
26 triangle3d_f(screen, POLYTYPE_GCOL, NULL, &v1, &v2, &v3);
27
28 textout_centre_ex(screen, font, "Hello, world!", 160, 96, makecol(0, 0, 0), -1);

So I just threw some coordinates there. For each vertex z is zero. It was by no means an equilateral triangle. It would have taken some more minutes to find out the coordinates for it. I would probably use a class I have for rotating coordinates.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

karistouf
Member #5,126
October 2004
avatar

woaw... quickly done ! thanks a lot for all your help.
this rocks... i was perparing myself for a painfull time trying to adapt barywhitecentric esoteric equations.... didnt now anything about this magnifical type of POLYTYPE GCOL.....

merci one thosuand times:):):)!

GullRaDriel
Member #3,861
September 2003
avatar

barywhitecentric: I am perhaps the only one to get that one as it should be, hilarious !

C'est quand même cool le forum allegro, il est rare de ne pas y trouver de l'aide ou une direction pour chercher !
OK, j'avoue, j'ai hâte de voir ton nouveau projet, stouf !

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Go to: