Aha! You basically want a color tint. The 100% formula for this is:
Brightness := (original.R + original.G + original.B)/(3*255) // normalized to 0..1
NewColor :=
RGB(tint.R*Brightness, tint.G*Brightness, tint.B*Brightness)
That is what jfheins already wrote, I suppose your implementation had rounding issues or did not normalize brightness (I'm too lazy to verfy this now
).
This should do the trick:
Delphi-Quellcode:
var
Bits: PColor32Entry;
Color: TColor32Entry;
I, J: Integer;
Percent: Single;
Brightness: Single;
begin
Color.ARGB := Color32(AColor);
Percent := APercent / 100;
Bits := @ASource.Bits[0];
for I := 0 to ASource.Height - 1 do
begin
for J := 0 to ASource.Width - 1 do
begin
Brightness := (Bits.R+Bits.G+Bits.B)/765;
Bits.R := Lerp(Bits.R, Round(Brightness * Color.R), Percent);
Bits.G := Lerp(Bits.G, Round(Brightness * Color.G), Percent);
Bits.B := Lerp(Bits.B, Round(Brightness * Color.B), Percent);
Inc(Bits);
end;
end;
ASource.Changed;
end;
As a refinement, you could calculate the brightness weighted according to the suggestion by the JPEG commité, that accounts for green being percieved brightest, followed by red and blue darkest at equal values.
The line
Brightness := (Bits.R+Bits.G+Bits.B)/765;
would then look like
Brightness := (0.299*Bits.R + 0.587*Bits.G + 0.114*Bits.B)/765;
It's a few operations more, but might improve the result notably.
"When one person suffers from a delusion, it is called insanity. When a million people suffer from a delusion, it is called religion." (Richard Dawkins)