### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Combine two colors  (Read 1755 times)

#### bigeno

• Sr. Member
• Posts: 250
##### Combine two colors
« on: December 01, 2018, 05:55:42 pm »
I've some selection on spreadsheet and some cell colors. How to combine selection color with colored cells ? example below. I simply using canvas.brush.color for cell background.

#### wp

• Hero Member
• Posts: 7553
##### Re: Combine two colors
« Reply #1 on: December 01, 2018, 07:08:49 pm »
Is this an fpspreadsheet question? The screenshot looks like that.

In fpspreadsheet the color of the selected cells is determined by this function from the color c of the cell:
Code: Pascal  [Select][+][-]
1. function CalcSelectionColor(c: TColor; ADelta: Byte) : TColor;
2. type
3.   TRGBA = record R,G,B,A: Byte end;
4. begin
5.   c := ColorToRGB(c);
6.   TRGBA(Result).A := 0;
7.   if TRGBA(c).R < 128
8.     then TRGBA(Result).R := TRGBA(c).R + ADelta
9.     else TRGBA(Result).R := TRGBA(c).R - ADelta;
10.   if TRGBA(c).G < 128
11.     then TRGBA(Result).G := TRGBA(c).G + ADelta
12.     else TRGBA(Result).G := TRGBA(c).G - ADelta;
13.   if TRGBA(c).B < 128
14.     then TRGBA(Result).B := TRGBA(c).B + ADelta
15.     else TRGBA(Result).B := TRGBA(c).B - ADelta;
16. end;
This function adds ADelta to each dark color component (i.e. makes it brigher) and subtracts ADelta from a bright color component (i.e. makes it darker). ADelta should not be too large, definitely less than 128; in the worksheet grid the value is 16.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### bigeno

• Sr. Member
• Posts: 250
##### Re: Combine two colors
« Reply #2 on: December 01, 2018, 08:46:47 pm »
No, thats screenshot from MS Excel, I work with TStringGrid.
Thank you, you right, I don't need mix two colors, darker will be ok in that case

#### Blaazen

• Hero Member
• Posts: 2922
• POKE 54296,15
##### Re: Combine two colors
« Reply #3 on: December 01, 2018, 09:13:42 pm »
I use this in ECControls:
Code: Pascal  [Select][+][-]
1. function GetMergedColor(AColor, BColor: TColor; AProportion: Single): TColor;
2. var aR, bR, aG, bG, aB, bB: Integer;
3. begin
4.   GetRGBIntValues(ColorToRGB(AColor), aR, aG, aB);
5.   GetRGBIntValues(ColorToRGB(BColor), bR, bG, bB);
6.   aR := bR + trunc((aR - bR)*AProportion);
7.   aG := bG + trunc((aG - bG)*AProportion);
8.   aB := bB + trunc((aB - bB)*AProportion);
9.   Result := RGBToColor(aR, aG, aB);
10. end;

Parameter AProportion should be between 0..1, where 1 means AColor, 0 means BColor.
Lazarus 2.1.0 r63940 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
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/