Page 1 of 1
HTML color -> PB color -> libxlsxwriter color
Posted: Thu Feb 06, 2025 5:13 pm
by the.weavster
I have this procedure that turns a HTML color string into a PB color:
Code: Select all
Procedure.l ColorStringToNumber(col.s)
Define.l ncol = 0
Define.s xcol = ""
If Left(col, 1) = "#"
Define.s xcol = LSet(col, 7, "0")
Define.a nR = Val("$" + Mid(xcol, 2, 2))
Define.a nG = Val("$" + Mid(xcol, 4, 2))
Define.a nB = Val("$" + Mid(xcol, 6, 2))
ncol = RGB(nR, nG, nB)
Else
ncol = ColorStringToNumber(ColorNameToHex(col)) ; omitted for brevity
EndIf
ProcedureReturn ncol
EndProcedure
As PB's color has the RGB values in 3 bytes I actually thought it was going to work with libxlsxwriter straight off the bat which has its color values defined like this:
Throughout libxlsxwriter colors are specified using a Html style RGB integer value. For example with a Format object:
Code: Select all
// typedef uint32_t lxw_color_t
format_set_font_color(format, 0x3030E0);
Although I am getting a color in my spreadsheet cells it's not the color I expect. I have tried poking the RGB values into the last 3 bytes of a 32bit integer to see if that would work but again, I got a color but not the one I wanted
If somebody could point me in the right direction I'd be grateful

Re: HTML color -> PB color -> libxlsxwriter color
Posted: Thu Feb 06, 2025 6:01 pm
by SMaag
It can't be that complicated.
What's the format of the col$
can you give me some examples of col$?
I have tried poking the RGB values into the last 3 bytes of a 32bit integer to see if that would work
you have to poke it in the first 3 Bytes
here is the Systens Colordefintion Structure for different Systems
Code: Select all
CompilerIf #PB_Compiler_Processor = #PB_Processor_PowerPC
; ----------------------------------------------------------------------
; This is the exception Alpha first : Motorola PowerPC
; ----------------------------------------------------------------------
Structure TSystemRGBA ; here it's ABGR in Memory
A.a
B.a
G.a
R.a
EndStructure
CompilerElse ; x86, x64; ARM32; ARM64
; ----------------------------------------------------------------------
; This is the standard Windows alignment RED=LoByte .. Alpha=HiByte
; In Memory RGBA but in Processor Register ABGR
; ----------------------------------------------------------------------
Structure TSystemRGBA ; here it's RGBA in Memory
R.a
G.a
B.a
A.a
EndStructure
CompilerEndIf
Structure TSystemColor ; The SystemColorStructure to have multiple access to the Color Channels!
; This is the trick how it works! TSystem RGBA is integrated as complete Structure, not as Pointer!
; This is a documented feature. See the PB help for Structure Union. There you will find the RGB example!
; IDENTICAL POINTERS FOR:
; @TSystemColor= @TSystemColor\col= @TSystemColor\RGB = @TSystemColor\RGB\R = @TSystemColor\ch[0]
StructureUnion
RGB.TSystemRGBA ; Access single color channels by name
col.l ; Access as 32Bit Color
ch.a[4] ; Access single color channels by number [0..3]
EndStructureUnion
EndStructure
Re: HTML color -> PB color -> libxlsxwriter color
Posted: Thu Feb 06, 2025 6:11 pm
by pfnuesu
What about this?
Code: Select all
Define xcolor.s
xcolor = (ReplaceString("#3030E0","#","$"))
Debug Red(Val(xcolor))
Debug Blue(Val(xcolor))
Debug Green(Val(xcolor))
;And back to hex to verify...
Debug Hex(RGB(Red(Val(xcolor)),Green(Val(xcolor)),Blue(Val(xcolor))))
Re: HTML color -> PB color -> libxlsxwriter color
Posted: Thu Feb 06, 2025 6:15 pm
by RASHAD
Your procedure works fine as expected
So probably libxlsxwriter using BGR not RGB
Re: HTML color -> PB color -> libxlsxwriter color
Posted: Thu Feb 06, 2025 6:20 pm
by SMaag
converting is correct
Code: Select all
Procedure.l ColorStringToNumber(col.s)
Protected.l ncol, nR, nG, nB
Protected.s xcol
col = Trim(col)
If Asc(col) = '#'
xcol = LSet(col, 7, "0")
Debug "xcol = " + xcol
nR = Val("$" + Mid(xcol, 2, 2))
nG = Val("$" + Mid(xcol, 4, 2))
nB = Val("$" + Mid(xcol, 6, 2))
Debug "nR = " + Hex(nR) + " = " + Str(nR)
Debug "nG = " + Hex(nG) + " = " + Str(nG)
Debug "nG = " + Hex(nB) + " = " + Str(nB)
ncol = RGB(nR, nG, nB)
EndIf
ProcedureReturn ncol
EndProcedure
#IndianRed = "#CD5C5C" ; RGB(205, 92, 92)
#LightCoral = "#F08080" ; RGB(240, 128, 128)
#Salmon = "#FA8072" ; RGB(250, 128, 114)
#DarkSalmon = "#E9967A" ; RGB(233, 150, 122)
#LightSalmon ="#FFA07A" ; RGB(255, 160, 122)
Define mycol.l, mycol$
Debug "Test for HTML IndianRed = #CD5C5C ; RGB(205, 92, 92)"
mycol = ColorStringToNumber(#IndianRed)
Debug "mycol = " + Hex(mycol)
Debug "R = " + Hex(Red(mycol))
Debug "G = " + Hex(Green(mycol))
Debug "B = " + Hex(Blue(mycol))
Re: HTML color -> PB color -> libxlsxwriter color
Posted: Thu Feb 06, 2025 6:30 pm
by the.weavster
RASHAD wrote: Thu Feb 06, 2025 6:15 pm
So probably libxlsxwriter using BGR not RGB
I don't think so as the declaration of cyan in libxlsxwriter's
format.h header file is:
Which matches
cyan in HTML: #00FFFF
I have tried using PB's RGB() directly, also poking to the first 3 bytes, poking to the last 3 bytes, reversing, etc... Whatever I've tried I never get the color I expect

Re: HTML color -> PB color -> libxlsxwriter color
Posted: Thu Feb 06, 2025 6:59 pm
by SMaag
LXW_COLOR_CYAN = 0x00FFFF
Cyan = RGB(0,255,255)
so 0x00FFFF =
RRGGBB is BigEndian and will reult in
BBGGRR in the Memory of x68, x64, PB works with little Endian RRGGBB
https://libxlsxwriter.github.io/working ... olors.html
blue = 0x0000FF -> BigEndian Blue PB use LittleEndian blue = 0xFF0000
so libxlsxwriter use BGR from the view of PureBasic
Re: HTML color -> PB color -> libxlsxwriter color
Posted: Thu Feb 06, 2025 7:15 pm
by the.weavster
Thank you both
I actually thought I had tried reversing RGB but I'd obviously slipped up doing it because that is working now

Re: HTML color -> PB color -> libxlsxwriter color
Posted: Thu Feb 06, 2025 11:32 pm
by AZJIO