1.How to change RGB color value to HSL color value?

2.How to reality photoshop's color effect, like "mergy" "screen" "darkness" "hard light" "soft light" etc...

I couldn't do it perfect like photoshop does when I use bitblt API's mode.

###### Who is Participating?

Commented:
with this you can convert RGB to HSL, HSL to RGB

var
HSLRange : integer = 240;

// convert a HSL value into a RGB in a TColor
// HSL values are 0.0 to 1.0 double
function HSLtoRGB (H, S, L: double): TColor;

// convert a HSL value into a RGB in a TColor
// SL values are 0 to the HSLRange variable
// H value is to HSLRange-1
function HSLRangeToRGB (H, S, L : integer): TColor;

// convert a RGB value (as TColor) into HSL
// HSL values are 0.0 to 1.0 double
procedure RGBtoHSL (RGB: TColor; var H, S, L : double);

// convert a RGB value (as TColor) into HSL
// SL values are 0 to the HSLRange variable
// H value is to HSLRange-1
procedure RGBtoHSLRange (RGB: TColor; var H, S, L : integer);

implementation

function HSLtoRGB (H, S, L: double): TColor;
var
M1,
M2: double;

function HueToColourValue (Hue: double) : byte;
var
V : double;
begin
if Hue < 0 then
Hue := Hue + 1
else
if Hue > 1 then
Hue := Hue - 1;

if 6 * Hue < 1 then
V := M1 + (M2 - M1) * Hue * 6
else
if 2 * Hue < 1 then
V := M2
else
if 3 * Hue < 2 then
V := M1 + (M2 - M1) * (2/3 - Hue) * 6
else
V := M1;
Result := round (255 * V)
end;

var
R,
G,
B: byte;
begin
if S = 0 then
begin
R := round (255 * L);
G := R;
B := R
end else begin
if L <= 0.5 then
M2 := L * (1 + S)
else
M2 := L + S - L * S;
M1 := 2 * L - M2;
R := HueToColourValue (H + 1/3);
G := HueToColourValue (H);
B := HueToColourValue (H - 1/3)
end;

Result := RGB (R, G, B)
end;

function HSLRangeToRGB (H, S, L : integer): TColor;
begin
Result := HSLToRGB (H / (HSLRange-1), S / HSLRange, L / HSLRange)
end;

// Convert RGB value (0-255 range) into HSL value (0-1 values)

procedure RGBtoHSL (RGB: TColor; var H, S, L : double);

function Max (a, b : double): double;
begin
if a > b then
Result := a
else
Result := b
end;

function Min (a, b : double): double;
begin
if a < b then
Result := a
else
Result := b
end;

var
R,
G,
B,
D,
Cmax,
Cmin: double;

begin
R := GetRValue (RGB) / 255;
G := GetGValue (RGB) / 255;
B := GetBValue (RGB) / 255;
Cmax := Max (R, Max (G, B));
Cmin := Min (R, Min (G, B));

// calculate luminosity
L := (Cmax + Cmin) / 2;

if Cmax = Cmin then  // it's grey
begin
H := 0; // it's actually undefined
S := 0
end else begin
D := Cmax - Cmin;

// calculate Saturation
if L < 0.5 then
S := D / (Cmax + Cmin)
else
S := D / (2 - Cmax - Cmin);

// calculate Hue
if R = Cmax then
H := (G - B) / D
else
if G = Cmax then
H  := 2 + (B - R) /D
else
H := 4 + (R - G) / D;

H := H / 6;
if H < 0 then
H := H + 1
end
end;

procedure RGBtoHSLRange (RGB: TColor; var H, S, L : integer);
var
Hd,
Sd,
Ld: double;
begin
RGBtoHSL (RGB, Hd, Sd, Ld);
H := round (Hd * (HSLRange-1));
S := round (Sd * HSLRange);
L := round (Ld * HSLRange);
end;
0

Author Commented:
it's very urgent! pls!
if I solve this problem ,i can give you 500 points more.
0

Commented:
hi venne,

see at
http://www.efg2.com/Lab/

http://www.efg2.com/Lab/Graphics/Colors/ShowImage.htm

meikl
0

Commented:
You're going to have to go down to a pixel level and modify the images directly (BitBlt's won't do the job). To keep things fast, use scanlines. As for individual effects then it all depends on what you want to do.

Let me know what you want and I'll see what I can do

The Neil =:)
0

Commented:
You're going to have to go down to a pixel level and modify the images directly (BitBlt's won't do the job). To keep things fast, use scanlines. As for individual effects then it all depends on what you want to do.

Let me know what you want and I'll see what I can do

The Neil =:)
0

Commented:
to expensive to miss, listening...
0

Commented:
Listening...
0

Commented:
listening....
0

Commented:
Take a look at the component at http://www.jps.net/gfody/

Goto The "Delphi Stuff" tree node.

Kevin
0

Commented:
just listening
0

Author Commented:
Hi, TheNeil:

Thanks. I know I must do it in pixel level. But I don't know the algorithm about pixel caculating.

for example, I have a personal photo, I want use
red color mix the face skin's color in photo without changing the face's lightness and darkness. It's look like  the man is making up.

How Can I do ?

0

Commented:
Well in your example I'd suggest increasing the red elements while decreasing the green and blue. Something along the lines of:

r := r + 2;
g := g - 1;
b := b - 1;

Then make sure that all of the values are still in the range 0 to 255. Normally I'd just suggest increasing the red value so you might need to ignore the modifications to the green and blue values.

Try this:

CONST
MaxPixelCount   =  32768;

TYPE
pRGBArray = ^TRGBArray;
TRGBArray = ARRAY[0..MaxPixelCount-1] OF TRGBTriple;
....

VAR
PixelArray : TRGBArray;
....

bmpTemp.PixelFormat := pf24bit;
FOR iY := 0 TO (bmpTemp.Height - 1)
DO
BEGIN
PixelArray := bmpTemp.Scanline[iY];
FOR iX := 0 TO (bmpTemp.Width - 1)
DO
BEGIN
Pixel := PixelArray[iX];
r := Pixel MOD 256;
g := (Pixel MOD 65536) DIV 256;
b := Pixel DIV 65536;

r := r + 2;
IF r > 255 THEN r := 255;
Pixel := RGB(r, g, b);
PixelArray[iX] := Pixel;
END;
END;

This is using scanlines as well so it's really fast. I don't know if it will solve your red face problem but this is the basic principle for doing most pixel manipulation

The Neil
0

Commented:
Hi Venne,

here's code to adjust an image's colors on the fly. It's advantage is that it is fast, the disadvantage that it does not care about preserving luminance.

procedure Coloring(Source: TBitmap; Red, Green, Blue: Integer);

// adjusts colors in the given bitmap according to the values passed in (useful range is -255..+255)

var Table: array[0..255]of TRGB;
X, Y: Integer;
I: Byte;
TargetPixel: PRGB;

begin
for I := 0 to 255 do
begin
Table[I].b := IntToByte(I + Blue);
Table[I].g := IntToByte(I + Green);
Table[I].r := IntToByte(I + Red);
end;

for Y := 0 to Source.Height - 1 do
begin
TargetPixel := Source.ScanLine[Y];
for X := 0 to Source.Width - 1 do
begin
TargetPixel.b := Table[TargetPixel.b].b;
TargetPixel.g := Table[TargetPixel.g].g;
TargetPixel.r := Table[TargetPixel.r].r;
Inc(TargetPixel);
end;
end;
end;

function IntToByte(Value: Integer): Byte;

begin
if Value < 0 then Result:= 0
else
if Value > 255 then Result := 255
else Result := Value;
end;

IntToByte can be replaced by a smarter construct:

ByteValue := Max(0, Min(255, IntValue));

See if this code is enough for your images, otherwise we can work out a special solution using a different color scheme, like HSL.

Ciao, Mike
0

Commented:
I remember I saw a delphi app. to do some of those effects.. it was in delphi super page I think, in full projects
or in delphi.about.com can't remember right now..
but I'm not sure if it came with source
0

Commented:
Hi SERENNE, welcome to Experts-Exchange. I haven't not said anything in the first question you answered but this time I think it is better to say a few words.

Please, here at E-E it is common practise to give only comments instead locking a question with an answer. This is in particular important when other experts have already contributed to the question. Don't be afraid that you will not receive points if they are well earned, because the quesioner gets links for every comment to accept it as answer and can so pick a one. Additionally you have only answer one small part of the entire question.

Thank you for listening.

Ciao, Mike
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.