Recent

Author Topic: [Solved] Darken TColor  (Read 4462 times)

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1248
[Solved] Darken TColor
« on: June 26, 2014, 09:36:35 am »
I'm ignorant in the world of image processing.   I'm highlighting rows in a grid by setting Color in OnPrepareCanvas (according to calculated criteria - bad = red, good = green, OKish = Yellow or Orange.  Unimportant = white.  The unimportant selected rows are highlighted in light gray automatically for me by the grid itself)

If the row is the selected row, I want to to highlight the row with a slighter darker version of the color.  Instead of hard-coding alternatives, is there a simple DarkenColor(AColor: TColor; APercent: Double): TColor anywhere?  (Or equivalent)

I've looked, and the ones I can find are in dedicated image processing libraries, which I *think* involves introducing levels of complexity beyond what I'm actually after...
« Last Edit: June 26, 2014, 06:28:49 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: Darken TColor
« Reply #1 on: June 26, 2014, 10:04:45 am »
use GraphUtil.ColorAdjustLuma or search for colorblend for alphablending two colors with a standart alpha value creating something like a chromasity ee a blue window on top of the back color.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 6235
Re: Darken TColor
« Reply #2 on: June 26, 2014, 10:06:42 am »
That's the way I did it in fpspreadsheetgrid. It darkens a bright color and brightens a dark color:

Code: [Select]
function CalcSelectionColor(c: TColor; ADelta: Byte) : TColor;
type
  TRGBA = record R,G,B,A: Byte end;
begin
  c := ColorToRGB(c);
  TRGBA(Result).A := 0;
  if TRGBA(c).R < 128
    then TRGBA(Result).R := TRGBA(c).R + ADelta
    else TRGBA(Result).R := TRGBA(c).R - ADelta;
  if TRGBA(c).G < 128
    then TRGBA(Result).G := TRGBA(c).G + ADelta
    else TRGBA(Result).G := TRGBA(c).G - ADelta;
  if TRGBA(c).B < 128
    then TRGBA(Result).B := TRGBA(c).B + ADelta
    else TRGBA(Result).B := TRGBA(c).B - ADelta;
end;
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1248
Re: Darken TColor
« Reply #3 on: June 26, 2014, 10:34:46 am »
Many thanks @taazz and @wp, I'll play with both solutions :-)
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: Darken TColor
« Reply #4 on: June 26, 2014, 11:12:57 am »
Quote
use GraphUtil.....
I didn't know this unit at all! I reinvented some methods in EC-Controls (NormalizeRect, GetMonochromaticColor, maybe more). I'll refactore the code.
Also unit GraphMath is interesting.
Anyway, thanks.
Lazarus 2.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1248
Re: Darken TColor
« Reply #5 on: June 26, 2014, 11:21:41 am »
Quote
use GraphUtil.....
I didn't know this unit at all!

There's a bucketload of undiscovered goodies all around :-)

And I've just noticed your tagline
Quote
POKE 54296,15
Wow, that takes me back :-)
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1248
Re: Darken TColor
« Reply #6 on: June 26, 2014, 06:28:04 pm »
Again, many thanks all.  I ended up going with
Code: [Select]
oColor := GetHighLightColor(oColor, 32);GetHighlightColor is defined in GraphUtil.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1248
Re: [Solved] Darken TColor
« Reply #7 on: August 29, 2018, 07:41:00 pm »
That's the way I did it in fpspreadsheetgrid. It darkens a bright color and brightens a dark color:

Apologies for re-opening an old topic, but thanks are due:   I just had to fix an issue where I was darkening a row all the way to blackness.  I resolved by popping back here and using @wp's solution instead.  So many thanks @wp :-)
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines