Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Problem/error with the result from al_color_rgb_to_html()?

Credits go to Arthur Kalliokoski and Jeff Bernard for helping out!
This thread is locked; no one can reply to it. rss feed Print
Problem/error with the result from al_color_rgb_to_html()?
TeaRDoWN
Member #8,518
April 2007
avatar

Trying to use the void al_color_rgb_to_html(float,float,float,char*) function but the output hex color code is incorrect. It appears that no matter what the third parameter is (red color) the result is "00" in the hex color. I've tested sending in everything from 0 to 1 but the result is always "00".

Reading the example at http://alleg.sourceforge.net/a5docs/refman/color.html#al_color_html

Example: al_color_rgb_to_html(1, 0, 0, html);

The result is: #ff0000

But if I change red color value from 0 to 1: al_color_rgb_to_html(1, 0, 1, html);

Result is still: #ff0000, instead of #ff00ff

Changing the green (or red) color value will however work as intended.

Anyone got this function to work?

Jeff Bernard
Member #6,698
December 2005
avatar

The code for the function is wrong. It is casting blue before multiplying:

void al_color_rgb_to_html(float red, float green, float blue,
    char *string)
{
   sprintf(string, "#%02x%02x%02x", (int)(red * 255),
      (int)(green * 255), (int)blue * 255); // <-- Wrong
}

There's another bug where it accepts color components greater than 1 or less than zero, it should be probably be changed to something like:

void al_color_rgb_to_html(float red, float green, float blue,
    char *string)
{
   // if you expect people might provide bad input:
   if (sizeof(string) > 7) {
      red = 0 > red ? 0 : 1 < red ? 1 : red;
      green = 0 > green ? 0 : 1 < green ? 1 : green;
      blue = 0 > blue ? 0 : 1 < blue ? 1 : blue;
   //---
      snprintf(string, 8, "#%02x%02x%02x", (int)(red * 255),
         (int)(green * 255), (int)(blue * 255)); // <-- Fix the cast, at least
   //---
   }
}

--
I thought I was wrong once, but I was mistaken.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Good catch!

They all watch too much MSNBC... they get ideas.

TeaRDoWN
Member #8,518
April 2007
avatar

I guess we are not talking "day 1 patch" for this. If/when fixed, when could a that version of Allegro be available?

pkrcel
Member #14,001
February 2012

latest GIT will almost surely take care of this bug very soon (i.e. as soon as a dev picks this up), but you'll have to compile allegro yourself.....for an official release you'll have to wait word of the devs.

On the other hand....wow I guess this function was not really used huh?

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Peter Wang
Member #23
April 2000

This function should never have made it into 5.0. Actually, the whole color addon.

SiegeLord
Member #7,827
October 2006
avatar

pkrcel said:

On the other hand....wow I guess this function was not really used huh?

I think I used it's inverse once.

Actually, the whole color addon.

Aww... The HSL/HSV functions are pretty useful I find.

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

Elias
Member #358
May 2000

I find the X11 color names most useful myself - but that's little surprise I guess :)

--
"Either help out or stop whining" - Evert

pkrcel
Member #14,001
February 2012

This function should never have made it into 5.0. Actually, the whole color addon.

the two following posts let me wonder....does this mean that there should be (have been) NO color addon at all?

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Peter Wang
Member #23
April 2000

It's just my personal opinion that most, if not all, the functionality provided by the color addon did not need to be provided by the Allegro core library or an official addon. Elias' initial commit wrote:

Quote:

Added a color addon for now, but this represents no decision to keep it in the official SVN repository, as its general usefulness is a bit borderline.

And there it stayed - and will stay.

Go to: