SRL Archive - SRL Archives

Color convert procedures


Lorax

2006-03-04 18:39:32

This is my latest work and I'm quite proud of it, maybe because it took me a hell lot of a time to find out what I needed and then where it was to be found and shiat.. Next ofcourse was the differences from the languages and some dumb mistakes.. Now I'm happy to present ColorConvert procedures by Lorax - hehe..

procedure Color32ToRGB(Color: Integer; var Red, Green, Blue: Integer);
begin
Blue := (Color and $00ff0000) shr 16;
Green := (Color and $0000ff00) shr 8;
Red := (Color and $000000ff);
end;

procedure RGBToColor32(var Color: Integer; Red, Green, Blue: Integer);
begin
Color := Blue shl 16;
Color := Color + Green shl 8;
Color := Color + Red;
end;

function Max3(int1, int2, int3: Extended): Extended;
begin
result:= MaxE(MaxE(int1, int2), MaxE(int2, int3));
end;

function Min3(int1, int2, int3: Extended): Extended;
begin
result:= MinE(MinE(int1, int2), MinE(int2, int3));
end;

procedure RGBToHSL(Red, Green, Blue: Integer; var Hue, Saturation, Luminance: Extended);
var
tRed, tGreen, tBlue, tMax, tMin, Delta: Extended;
begin
tRed:= Red div 255;
tGreen:= Green div 255;
tBlue:= Blue div 255;

tMax:= Max3(tRed, tBlue, tGreen);
tMin:= Min3(tRed, tBlue, tGreen);

Delta:= tMax - tMin;

Luminance:= (tMax + tMin) div 2.0;

if(tMax = tMin)then
begin
Saturation:= 0.0;
Hue:= 0.0;
end else
begin

if Luminance < 0.5 then
Saturation:= Delta div (2 * Luminance)
else
Saturation:= Delta div (2 - tMax - tMin);

if(tMax = tRed)then
Hue:= 0 + (tGreen - tBlue) div Delta;
if(tMax = tGreen)then
Hue:= 2 + (tBlue - tRed) div Delta;
if(tMax = tBlue)then
Hue:= 4 + (tRed - tGreen) div Delta;

Hue:= Hue div 6.0;
end;

Hue:= Hue * 100;
Saturation:= Saturation * 100;
Luminance:= Luminance * 100;
end;

function HueToRGB(temp1, temp2, Hue: Extended): Extended;
begin
if(Hue < 0)then
Hue:= Hue + 1;
if(Hue > 1)then
Hue:= Hue - 1;
if(6 * Hue < 1.0)then
result:= temp1 + (temp2 - temp1) * 6 * Hue
else if(2 * Hue < 1.0)then
result:= temp2
else if(3 * Hue < 2.0)then
result:= temp1 + (temp2 - temp1) * ((2.0 div 3.0) - Hue) * 6
else
result:= temp1;
end;

procedure HSLToRGB(var Red, Green, Blue: Integer; Hue, Saturation, Luminance: Extended);
var
tSaturation, tLuminance, tHue, temp2, temp1: Extended;
begin
tSaturation:= Saturation div 100.0;
tLuminance:= Luminance div 100.0;
tHue:= Hue div 100.0;

if(Saturation = 0)then
begin
Red := Round(tLuminance * 255);
Green := Round(tLuminance * 255);
Blue := Round(tLuminance * 255);
end else
begin

if(tLuminance < 0.5)then
temp2:= tLuminance * (1 + tSaturation)
else
temp2:= (tLuminance + tSaturation) - (tSaturation * tLuminance);

temp1:= 2 * tLuminance - temp2;

Red := Round(255 * HueToRGB(temp1, temp2, tHue + (1.0 div 3.0)));
Green := Round(255 * HueToRGB(temp1, temp2, tHue));
Blue := Round(255 * HueToRGB(temp1, temp2, tHue - (1.0 div 3.0)));
end;
end;

Sdcit

2006-03-04 19:06:37

woooooowww….. good job lorax!

The Prince of Randomness?

2006-03-04 19:17:17

Very nice Work Lorax.

XxKanexX

2006-03-04 20:38:07

Good job Lory D: <3

BenLand100

2006-03-05 03:04:39

After our interesting conversation how bout you explain the inner workings to everyone else :rolleyes:

Lorax

2006-03-05 09:52:44

From: BenLand100
After our interesting conversation how bout you explain the inner workings to everyone else :rolleyes:

Okay I think that is fair, here is the whole story.

1: I wanted to make a script that is to paint Mona Lisa, but for that I need to make all the colors from start have a predefined luminance.

2: I wanted to convert the color format SCAR used to HSL which paint uses.

3: So I started looking around trying to find an matching format. It took a half hour before I got an idea, so I decided to check out a procedure which I had found in the SCAR manual one month layter:
(function ConvertColor(C: Integer): Integer; - converts Windows RGB color to Graphics32 color. Depreciated.)

4. Graphics32 was a great break through and after some research I found their site. And a lot info about them and such.

5. I googled around trying to find any formulare that would convert an Graphics32 color to RGB and I got a hint from the developers site itself. It wasn't very clear though because it had the Alpha value and the format would be RGBA and not RGB. Layter on through examination I found out that the alpha value wasn't needed. Herefrom Color32ToRGB.

6. Now I needed a procedure to reverse this. And again I googled around and after 10 mins I stumbled across something that again was for RGBA, which I copied "Note I still used Color32ToRGBA at that time". It was when I had RGBAToColor32 and Color32ToRGBA that my research got bonus, after some minuts of searching for an convert procedure from RGBToColor32 (no result) I made my own research, founding out that the alpha value had no influence, therefore I could easily change format from RGBA to RGB, which was my wanted format for now.

7. But I needed the format HSL and not RGB, and again that lead me to a lot of research. Again Google was my friend I found out about easyRGB.com through a lot of different sites, and they themselves had the formula for converting RGB colors to HSL and HSL to RGB.

8. I just had one problem, I combined two different procedures I had RGBToHSL from one site and HSLToRGB from another, of course they were in different languages so I had a lot of modifying to do. The main problem was that the max value of Hue, Saturation and Luminance is not the same at all places, sometimes they're 1 sometimes 360(degrees) and sometimes 6, like in my case. So I had to actually make the Hue format different.. Note that the max value of Hue at the place from where I got RGBToHSL, were hard to find..

8. So once I had got the right format I had another problem

9. the formula from easyRGB.com was in VB, and in VB once you divide 1 with 3 you get 0.33333 but in SCAR you get 0. It took my mate Liquid 2 mins to figure this out and it was only once I asked him about it I figured it out myself. in SCAR 1.0 div 3.0 gives 0.3333333 that is because the format is float and not an integer like in 1 div 3 and the calculation way is different.

But AFTER like 4-5 hours of research and mind trouble I got it right..

Edit: oh and Kane "lory" is a terrible nick name hehe

Cheesehunk

2006-03-05 12:05:21

Wow, very interesting, I would have never been able to think up somethign like this, especially attempting at doing it!!

Awesome job Lorax!

BenLand100

2006-03-05 12:46:23

It just goes to show you can do anything with google :rolleyes:

Sdcit

2006-03-05 12:52:59

come on ben :p be nice. loraz is just teasing you

Lorax

2006-03-05 13:47:07

From: BenLand100
It just goes to show you can do anything with google :rolleyes:

If I may correct you, this shows that anything can be done with an awesome brain and google :)

BenLand100

2006-03-05 13:56:24

From: Lorax
If I may correct you, this shows that anything can be done with an awesome brain and google :)

You just seemed to have missed the part of actualy doing the conversion in your explination :rolleyes:

Lorax

2006-03-05 14:21:06

From: BenLand100
You just seemed to have missed the part of actualy doing the conversion in your explination :rolleyes:

what are you talking about ?!

Sdcit

2006-03-05 14:26:28

people. please stop arguing/teasing/whatever, its counterproductive.

Spky

2006-03-05 15:09:58

Wow, very nice !

Lorax

2006-03-06 05:58:29

From: Sdcit
people. please stop arguing/teasing/whatever, its counterproductive.

Don't worry me and benland are just kidding around.. We've had our differences but they're gone. I got nothing on benland..

Liquid

2006-03-06 07:12:10

And Liquid. ;)

Lorax

2006-03-06 14:56:33

From: Liquid
And Liquid. ;)

Just wait Liquid, I will dig something up * catches phone and dials special agent*