Recent

Author Topic: [SOLVED] Stored Hexadecimal Colors  (Read 5928 times)

Qoz

  • Newbie
  • Posts: 2
[SOLVED] Stored Hexadecimal Colors
« on: September 12, 2016, 07:25:42 pm »
Hello all, I'm fairly new with Lazarus, but I'm typically able to troubleshoot the oddities I encounter. However, I have a particular issue I've been unable to solve for a few days now and I'm not really sure what else to try.

I need to create a TShape with colors stored in variables, but the color doesn't translate properly. Here is a rudimentary example of what I'm attempting to accomplish:
Code: Pascal  [Select][+][-]
  1. var
  2.    colorA:Integer;
  3.  
  4. begin
  5.    colorA := $0033CCFF;
  6.    myShape.Brush.Color := colorA;
  7. end;

This results in myShape turning RGB(178,223,15) which is green, while the color I'm saving in colorA is RGB(255,204,102) which is yellow.

As a test:
Code: Pascal  [Select][+][-]
  1. myShape.Brush.Color := $0033CCFF;
myShape is correctly transformed into the desired color.

My question is simply how can I store colors in variables correctly? I've tried different types such as ShortInt and Cardinal, and something like:
Code: Pascal  [Select][+][-]
  1. var
  2.    colorA:String;
  3.  
  4. begin
  5.    colorA := '0033CCFF';
  6.    myShape.Brush.Color := StrToInt('$' + colorA);
  7. end;
Just returns an error.

Furthermore, I've noticed there are some inconsistencies with the hexadecimal color codes Lazarus outputs compared to what I find in Photoshop. For example, the yellow color I'm interested in (RGB(255,204,102)) has the following hexadecimal values:
Via Photoshop: FFCC66
Via Lazarus: 0033CCFF

When I remove the 00 alpha and enter '33CCFF' into Photoshop, I get an RGB(51, 204, 255) which is blue.

Any help offered is greatly appreciated, I'm always eager to learn and I'm hoping I'm just missing something very simple! Also, it's important to note that I'm interested only in using very specific colors, not defaults such as clYellow.
« Last Edit: September 12, 2016, 09:23:32 pm by Qoz »

lainz

  • Hero Member
  • *****
  • Posts: 4742
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Stored Hexidecimal Colors
« Reply #1 on: September 12, 2016, 07:38:33 pm »
I always click on the color button to choose and input each R G B value to get the exact number that is used by lazarus.

Or I use the function RGBToColor(R, G, B: Byte): TColor;
« Last Edit: September 12, 2016, 07:42:57 pm by lainz »

wp

  • Hero Member
  • *****
  • Posts: 13486
Re: Stored Hexadecimal Colors
« Reply #2 on: September 12, 2016, 07:43:50 pm »
Looks that Photoshop flips the R and B bytes in presentation:
- Photoshop RRGGBB (like html)
- Lazarus: $00BBGGRR
i.e. Photoshop Yellow FFCC66 translates to $0066CCFF for Lazarus
« Last Edit: September 12, 2016, 07:46:49 pm by wp »

Fungus

  • Sr. Member
  • ****
  • Posts: 354
Re: Stored Hexadecimal Colors
« Reply #3 on: September 12, 2016, 07:49:30 pm »
That is because pascal uses "back words" numeric values. You can correct the colors like this:

Code: Pascal  [Select][+][-]
  1. Function CorrectColor(C: Cardinal): Cardinal;
  2. Begin
  3.   Result:= (C Shr 24) Or ((C And $FF0000) Shr 8) Or ((C And $FF00) Shl 8) Or (C Shl 24);
  4. End;

Qoz

  • Newbie
  • Posts: 2
Re: Stored Hexidecimal Colors
« Reply #4 on: September 12, 2016, 09:23:18 pm »
Thank you all for your quick responses!

Or I use the function RGBToColor(R, G, B: Byte): TColor;
This works very well, and is exactly what I was looking for; thank you!

Looks that Photoshop flips the R and B bytes in presentation:
- Photoshop RRGGBB (like html)
- Lazarus: $00BBGGRR
i.e. Photoshop Yellow FFCC66 translates to $0066CCFF for Lazarus

That does appear to be so, I just wasn't using my brain! Thank you!

That is because pascal uses "back words" numeric values. You can correct the colors like this:

Code: Pascal  [Select][+][-]
  1. Function CorrectColor(C: Cardinal): Cardinal;
  2. Begin
  3.   Result:= (C Shr 24) Or ((C And $FF0000) Shr 8) Or ((C And $FF00) Shl 8) Or (C Shl 24);
  4. End;

I attempted to implement this, and while it does change the color, it appears to be arbitrary. This is very nifty though, so thank you for sharing!

 

TinyPortal © 2005-2018