Convert long color to hex values

Just starting out? Need help? Post your questions and find answers here.
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Convert long color to hex values

Post by WilliamL »

[edit - I had the color wrong! Yes, it was gray.)

I have a color that is 12249272 (light green) and I want to convert it to the hex values like $00FF to put into my RGB structure. Since Hex() returns a string it is not working for me. How can this be done? Oh, and one other thing, I need to repeat the 2 character string to end up with a 4 character string. For example if the hex is $FF I need $FFFF (a quirk of the API).

Code: Select all

Style\BackColor\Red = Red(15263976)*256
This works correctly.
Last edited by WilliamL on Sat Mar 05, 2011 11:32 pm, edited 3 times in total.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Convert long color to hex values

Post by Trond »

There is no difference between a normal number and a "hex" number inside memory. The difference is only in the display, which is why Hex() returns a string (if it returned a number it would simply be the original number).

The reason you can't get things to work is that you think 15263976 is a "light green colour", but in reality, this colour is grey. I don't know why you think it's green.

You can clearly see it's gray, because the red, green and blue color components are all equal:

Code: Select all

c = 15263976
Debug Red(c)
Debug Green(c)
Debug Blue(c)
Try with 7070100, it's a nice light green colour.
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Convert long color to hex values

Post by WilliamL »

ok, so how do I make $B8 into $B8B8 using Red(12249272)?

I got it! I multiply by 257 and I get the number I want. Nice lite green.. very soothing. Maybe someday I will figure out why. :)
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Convert long color to hex values

Post by skywalk »

Code: Select all

color.i = 12249272 
Debug Hex(color)    ;                 $BAE8B8
cr.i = Red(color)   :Debug Hex(cr)  ; $B8 
cg.i = Green(color) :Debug Hex(cg)  ; $E8
cb.i = Blue(color)  :Debug Hex(cb)  ; $BA

cr$ = Hex(cr)
cg$ = Hex(cg)
cb$ = Hex(cb)
Debug Val("$"+cb$+cg$+cr$)          ; 12249272
; Observe the order of the hex colors are $blue+$green+$red
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Convert long color to hex values

Post by Shardik »

WilliamL wrote:I got it! I multiply by 257 and I get the number I want.
William,

you have figured it out by yourself... :wink:

Nevertheless I want to post a complete Mac example with a little procedure
which does the complete conversion from RGB values with byte values to RGB
with word values and displays a TextGadget with a light green background:

Code: Select all

ImportC ""
  SetControlFontStyle(ControlRef, *ControlFontStyleRec)
  SetControlVisibility(ControlRef, IsVisible, DoDraw)
EndImport

#kControlUseBackColorMask = 16

Structure RGBColor
  Red.U
  Green.U
  Blue.U
EndStructure

Structure ControlFontStyleRec
  Flags.W
  Font.W
  Size.W
  Style.W
  Mode.W
  Just.W
  ForeColor.RGBColor
  BackColor.RGBColor
EndStructure

Procedure ConvertRGB8ToRGB16(RGB8.L)
  Shared RGB16.RGBColor

  RGB16\Red = Red(RGB8) * 257
  RGB16\Green = Green(RGB8) * 257
  RGB16\Blue = Blue(RGB8) * 257
EndProcedure

Define RGB16.RGBColor
Define Style.ControlFontStyleRec

OpenWindow(0, 0, 0, 350, 38, "TextGadget with light green background", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TextGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20, "The quick brown fox jumps over the lazy dog.", #PB_Text_Border)

Style\Flags = #kControlUseBackColorMask
ConvertRGB8ToRGB16(7070100)
Style\BackColor = RGB16

SetControlFontStyle(GadgetID(0), @Style)
SetControlVisibility(GadgetID(0), #True, #True)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Convert long color to hex values

Post by Kukulkan »

Hello,

is this no longer working? I tried it with 5.22 LTS on MacOS with Maverics (10.9) but the background colour does not change.

Also,
* what colour is 7070100? Can I use HEX like $BBGGRR here? (can not test because it dos not work)
* Can I use this for CheckBox background, too?

Kukulkan
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Convert long color to hex values

Post by wilbert »

The code Shardik posted is from a long time ago and still Carbon based instead of Cocoa.
When using Cocoa, you can use setBackgroundColor:

Code: Select all

Procedure.i NSColorBGR(BGR.l)
  Protected.CGFloat r, g, b, a = 1
  Protected *byte.Ascii = @BGR
  r = *byte\a / 255: *byte + 1
  g = *byte\a / 255: *byte + 1
  b = *byte\a / 255
  ProcedureReturn CocoaMessage(0, 0, "NSColor colorWithCalibratedRed:@", @r, "green:@", @g, "blue:@", @b, "alpha:@", @a)
EndProcedure

Procedure.i NSColorRGB(RGB.l)
  Protected.CGFloat r, g, b, a = 1
  Protected *byte.Ascii = @RGB
  b = *byte\a / 255: *byte + 1
  g = *byte\a / 255: *byte + 1
  r = *byte\a / 255
  ProcedureReturn CocoaMessage(0, 0, "NSColor colorWithCalibratedRed:@", @r, "green:@", @g, "blue:@", @b, "alpha:@", @a)
EndProcedure

OpenWindow(0, 0, 0, 350, 38, "TextGadget with light green background", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TextGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20, "The quick brown fox jumps over the lazy dog.", #PB_Text_Border)

CocoaMessage(0, GadgetID(0), "setBackgroundColor:", NSColorBGR($6BE194))

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
For a checkbox, you can set an attributed title.
This allows you also to only set part of the title to a different background color using the range property.
To do the same on a TextGadget (only part of the text a background color), you can use setAttributedStringValue: instead of setAttributedTitle:

Code: Select all

Procedure.i NSColorBGR(BGR.l)
  Protected.CGFloat r, g, b, a = 1
  Protected *byte.Ascii = @BGR
  r = *byte\a / 255: *byte + 1
  g = *byte\a / 255: *byte + 1
  b = *byte\a / 255
  ProcedureReturn CocoaMessage(0, 0, "NSColor colorWithCalibratedRed:@", @r, "green:@", @g, "blue:@", @b, "alpha:@", @a)
EndProcedure

Procedure.i NSColorRGB(RGB.l)
  Protected.CGFloat r, g, b, a = 1
  Protected *byte.Ascii = @RGB
  b = *byte\a / 255: *byte + 1
  g = *byte\a / 255: *byte + 1
  r = *byte\a / 255
  ProcedureReturn CocoaMessage(0, 0, "NSColor colorWithCalibratedRed:@", @r, "green:@", @g, "blue:@", @b, "alpha:@", @a)
EndProcedure



OpenWindow(0, 0, 0, 350, 38, "CheckBoxGadget with light green background", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

CheckBoxLabel.s = "The quick brown fox jumps over the lazy dog."
CheckBoxGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20, CheckBoxLabel)

AttributedTitle = CocoaMessage(0, CocoaMessage(0, 0, "NSMutableAttributedString alloc"), "initWithString:$", @CheckBoxLabel)

Range.NSRange\length = Len(CheckBoxLabel)
CocoaMessage(0, AttributedTitle, "addAttribute:$", @"NSBackgroundColor", "value:", NSColorBGR($6BE194), "range:@", @Range)

CocoaMessage(0, GadgetID(0), "setAttributedTitle:", AttributedTitle)
CocoaMessage(0, AttributedTitle, "release")

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Convert long color to hex values

Post by Kukulkan »

Thank you Wilbert! Will give it a try on Monday!

Kukulkan
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Convert long color to hex values

Post by Shardik »

Kukulkan wrote:Hello,

is this no longer working? I tried it with 5.22 LTS on MacOS with Maverics (10.9) but the background colour does not change.
As Wilbert already explained my code from above is written for a PB version using the Carbon framework. Of course it's still working but for running it you have to use PB 5.11 x86, select "Compiler/Compiler Options..." in the menu bar and put subsystem "Carbon" behind "Library Subsystem:".

PB 5.11 x86 was the last PB version which supports Carbon as a subsystem. In PB 5.0 the Carbon framework was replaced by the Cocoa framework, although it was still possible to select Carbon as a subsystem until PB 5.11. The Carbon framework was never ported by Apple to 64 bit, so it was only possible to select Carbon as a subsystem in the 32-bit versions of PB.
Post Reply