Recent

Author Topic: Bright color  (Read 1876 times)

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 325
Bright color
« on: December 04, 2024, 09:14:39 pm »
Say this produces dark blue. Ideas how to convert it to light blue ?
Should work for any color randome generated.
Out of curiosity, what would pitch black then become ?

Code: Pascal  [Select][+][-]
  1. RGBToColor(Random(256),Random(256),Random(256))
  2.  
« Last Edit: December 04, 2024, 09:17:13 pm by BubikolRamios »
lazarus 3.2-fpc-3.2.2-win32/win64

Fibonacci

  • Hero Member
  • *****
  • Posts: 643
  • Internal Error Hunter
Re: Bright color
« Reply #1 on: December 04, 2024, 09:28:28 pm »
Code: Pascal  [Select][+][-]
  1. function MulDiv(a, b, c: int64): int64;
  2. begin
  3.   result := (a*b) div c;
  4. end;
  5.  
  6. function lighten(color, pc: dword): dword;
  7. var
  8.   r, g, b: byte;
  9. begin
  10.   r := color;
  11.   g := color shr 8;
  12.   b := color shr 16;
  13.   r := r+MulDiv(255-byte(color), pc, 100);
  14.   g := g+MulDiv(255-byte(color shr 8), pc, 100);
  15.   b := b+MulDiv(255-byte(color shr 16), pc, 100);
  16.   result := r or (g shl 8) or (b shl 16);
  17. end;
  18.  
  19. function RGBToColor(r, g, b: dword): dword;
  20. begin
  21.   result := r;
  22.   result := result or (g shl 8);
  23.   result := result or (b shl 16);
  24. end;
  25.  
  26. procedure TForm1.Button1Click(Sender: TObject);
  27. begin
  28.   form1.Canvas.Brush.Color := lighten(RGBToColor(Random(256),Random(256),Random(256)), 50);
  29.   form1.Canvas.FillRect(0, 0, 500, 500);
  30. end;

Code: Pascal  [Select][+][-]
  1. function darken(color, pc: dword): dword;
  2. var
  3.   r, g, b: byte;
  4. begin
  5.   r := color;
  6.   g := color shr 8;
  7.   b := color shr 16;
  8.   r := r-MulDiv(byte(color), pc, 100);
  9.   g := g-MulDiv(byte(color shr 8), pc, 100);
  10.   b := b-MulDiv(byte(color shr 16), pc, 100);
  11.   result := r or (g shl 8) or (b shl 16);
  12. end;

wp

  • Hero Member
  • *****
  • Posts: 12515
Re: Bright color
« Reply #2 on: December 04, 2024, 10:24:04 pm »
In unit GraphUtil:
Code: Pascal  [Select][+][-]
  1. function GetHighLightColor(const Color: TColor; Luminance: Integer = 19): TColor;
  2. function GetShadowColor(const Color: TColor; Luminance: Integer = -50): TColor;

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 325
Re: Bright color
« Reply #3 on: December 05, 2024, 07:42:07 am »
Quote
function GetHighLightColor(const Color: TColor; Luminance: Integer = 19): TColor;

Kind of works (doing FloodFill from current to luminated color on click), I assume the Luminance should be max 250, or ? How come it takes bigger numbers ? I assume 249 (source Luminance) results in 18, at default Luminance parameter 19 .

Code: Pascal  [Select][+][-]
  1.   PaintBox1.Canvas.Brush.Color:= GetHighLightColor(PaintBox1.Canvas.Pixels[b.X,b.y]);
  2.   PaintBox1.Canvas.FloodFill(b.X,b.y,PaintBox1.Canvas.Pixels[b.X,b.y],fsSurface);    
  3.  
lazarus 3.2-fpc-3.2.2-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 12515
Re: Bright color
« Reply #4 on: December 05, 2024, 10:06:04 am »
Kind of works (doing FloodFill from current to luminated color on click), I assume the Luminance should be max 250, or ? How come it takes bigger numbers ? I assume 249 (source Luminance) results in 18, at default Luminance parameter 19 .
I don't understand what you mean. The function converts the input color to the HLS color system and adds the provided Luminance parameter to the L value of the HLS input color. Since L in this function is a byte, the maximum L value is 255. The function does not check the byte range, however, and may lead to an ERange exception. (A bit careless, in my opinion...; and as I notriced, GetShadowColor does the same as GetHighlightColor). So, when you make too large luminance changes you may run into this issue.

Warfley

  • Hero Member
  • *****
  • Posts: 1838
Re: Bright color
« Reply #5 on: December 05, 2024, 10:29:06 am »
RGB is a bad format to manipulate colors. You can convert RGB to HSV where you have the Hue, which describes the color, Saturation which describes how colorful it is and Value which controls brightness.
For example Hue value 0 is red, with saturation 255 and value 255 you get a bright red. With saturation at 90 and value at 255 you get a Pink, while with value at 90 but saturation at 255 you get a dark bloody red

So for example you get a color in rgb, you convert it to HSV. Then you check if the Value is smaller than 128, then its a dark color. If you brighten it up, just increase that value. Similarly you can check using the saturation if it's more gray leaning or more colorful.

Especially if you want to generate a color scheme you can let the user pick a color (the hue value) and then generate a dark, a bright, a dim and a colorful version of it by tinkering the saturation and value
« Last Edit: December 05, 2024, 10:31:13 am by Warfley »

 

TinyPortal © 2005-2018