2005. július 2., szombat

Making adjustments to Delphi Colors


Problem/Question/Abstract:

How to make Delphi standard colors lighter or darker

Answer:

Here are some functions I use to make adjustments to the standard colors in Delphi.

The functions Darker and Lighter require 2 parameters and are used like this:

Panel1.Color := Darker(clBlue, 20);

This produces a panel color that is 20% darker than blue.

How it works:
Each of the three primary colors (Red,Green,Blue) can have values from 0 to 255 and can combine to form 16,777,216 possible colors. You can visualize the three primaries as the three axis' of a cube where the directions x, y and z correspond to the colors red, green and blue. Then each 3 dimensional point in the cube would represent one of the 16M colors. At the point in the cube where all the values are 0 (0,0,0) the color is black, and at (255,255,255) the color is white, (255,0,0) is pure red, etc.

If you visualize a line drawn between any color (r,g,b) and white (255,255,255)  then all the points that make up that line corespond to all valures of the color (r,g,b) as it becomes lighter and lighter until it reaches pure white.

That same for a line line drawn between any color (r,g,b) and black (0,0,0). The line represents all shades of that color as it darkens to pure black.

The function "Darker" returns a new color value that is the specified percentage closer to black. 100% is pure black.
The function "Lighter" returns a new color value that is the specified percentage closer to white. 100% is pure white.

function Darker(Color: TColor; Percent: Byte): TColor;
var
  r, g, b: Byte;
begin
  Color := ColorToRGB(Color);
  r := GetRValue(Color);
  g := GetGValue(Color);
  b := GetBValue(Color);
  r := r - muldiv(r, Percent, 100); //Percent% closer to black
  g := g - muldiv(g, Percent, 100);
  b := b - muldiv(b, Percent, 100);
  result := RGB(r, g, b);
end;

function Lighter(Color: TColor; Percent: Byte): TColor;
var
  r, g, b: Byte;
begin
  Color := ColorToRGB(Color);
  r := GetRValue(Color);
  g := GetGValue(Color);
  b := GetBValue(Color);
  r := r + muldiv(255 - r, Percent, 100); //Percent% closer to white
  g := g + muldiv(255 - g, Percent, 100);
  b := b + muldiv(255 - b, Percent, 100);
  result := RGB(r, g, b);
end;

I have also added these convenience functions that can be used like this:

Panel1.Color := Light(clBlue);
Panel1.Color := SlightlyDark(clRed);
Panel1.Color := VeryLight(clMagenta);
{etc. }

function SlightlyDark(Color: TColor): TColor;
begin
  Result := Darker(Color, 25);
end;

function Dark(Color: TColor): TColor;
begin
  Result := Darker(Color, 50);
end;

function VeryDark(Color: TColor): TColor;
begin
  Result := Darker(Color, 75);
end;

function SlightlyLight(Color: TColor): TColor;
begin
  Result := Lighter(Color, 25);
end;

function Light(Color: TColor): TColor;
begin
  Result := Lighter(Color, 50);
end;

function VeryLight(Color: TColor): TColor;
begin
  Result := Lighter(Color, 75);
end;

Nincsenek megjegyzések:

Megjegyzés küldése