Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Delphi/Lazarus - How to make a TColor lighter or darker

5 Posts
3 Users
1 Reactions
5,219 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

Changing TColor to darker and lighter ... did you ever try it? It can be a b*tch ...

Here two procedure that make it easier on you.
Usage:

Canvas.Brush.Color:=Lighter(clBlue,20); // sets the color to 20% lighter than clBlue

Note: under Lazarus you'll need to add "LclIntf" to your uses clause (GetRValue etc. are Windows calls and Lazarus is crossplatform)

function Darker(MyColor:TColor; Percent:Byte):TColor;
var r,g,b:Byte;
begin
MyColor:=ColorToRGB(MyColor);
r:=GetRValue(MyColor);
g:=GetGValue(MyColor);
b:=GetBValue(MyColor);
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(MyColor:TColor; Percent:Byte):TColor;
var r,g,b:Byte;
begin
MyColor:=ColorToRGB(MyColor);
r:=GetRValue(MyColor);
g:=GetGValue(MyColor);
b:=GetBValue(MyColor);
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;  


   
ReplyQuote
(@fenden)
New Member
Joined: 10 years ago
Posts: 1
 

Thank you. Your guide code is good. It's easy for me to customize the TColor.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

I forgot to mention one unit to include in the uses clause, so here the full list:

uses ... LclIntf, lcltype ...;

   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
 

Note: I am a little late to the thread, but here is what I use.  I write verbose delphi code with structure so it makes sense if I see it years later.

function GetColorOffSet( const AColor: TColor; const AOffSetPercentage: integer = 50 { AOffSetPercentage Range is 1..99} ): TColor;

  function SetValuesForRGB( var ALuminance: double; var ARed: integer; var AGreen: integer; var ABlue: integer ): boolean;
  var
    AResult: boolean;
    ARGB: LongInt;
  begin
    AResult := False;
    try
      try
        ARGB := ColorToRGB( AColor );
        ARed := GetRValue( ARGB );
        AGreen := GetGValue( ARGB );
        ABlue := GetBValue( ARGB );
        ALuminance := (0.2126 * ARed) + (0.7152 * AGreen) + (0.0722 * ABlue);
        AResult := True;
      except
        AResult := False;
      end;
    finally
      SetValuesForRGB := AResult;
    end;
  end;

  function GetOffSetColor( ALuminance: double; ARed: integer; AGreen: integer; ABlue: integer ): TColor;

    function ValidateRGB( AValue: integer ): integer;
    begin
      try
        if AValue > 255 then
        begin
          AValue := 255
        end
        else if AValue < 0 then
        begin
          AValue := 0;
        end;
      finally
        ValidateRGB := AValue;
      end;
    end;

  const
    csOnePercentColor = 2.55;
    cdHalfOfRGB = 127.5;
  var
    AResult: TColor;
  begin
    AResult := AColor;
    try
      try
        if ALuminance <= cdHalfOfRGB then { lighter increases RGB Values }
        begin
          ARed := ValidateRGB( Trunc( ARed + ( AOffSetPercentage * csOnePercentColor )));
          AGreen := ValidateRGB( Trunc( AGreen + ( AOffSetPercentage * csOnePercentColor )));
          ABlue := ValidateRGB( Trunc( ABlue + ( AOffSetPercentage * csOnePercentColor )));
        end
        else { darker decreases RGB Values }
        begin
          ARed := ValidateRGB( Trunc( ARed - ( AOffSetPercentage * csOnePercentColor )));
          AGreen := ValidateRGB( Trunc( AGreen - ( AOffSetPercentage * csOnePercentColor )));
          ABlue := ValidateRGB( Trunc( ABlue - ( AOffSetPercentage * csOnePercentColor )));
        end;
        AResult := RGB( ARed, AGreen, ABlue );
      except
        AResult := AColor;
      end;
    finally
      GetOffSetColor := AResult;
    end;
  end;

var
  AResult: TColor;
  ARed: integer;
  AGreen: integer;
  ABlue: integer;
  ALuminance: double;
begin
  AResult := AColor;
  try
    if (AOffSetPercentage > 0) and (AOffSetPercentage < 100) then
    begin
      if SetValuesForRGB( ALuminance, ARed, AGreen, ABlue ) then
      begin
        AResult := GetOffSetColor( ALuminance, ARed, AGreen, ABlue );
      end;
    end;
  finally
    GetColorOffSet := AResult;
  end;
end;

   
Hans reacted
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

Hi slincke!

Thank you for the contribution! Always good to have options right? 😊 

Note: I've edited your post to have it code formatted.


   
ReplyQuote
Share: