BASIC Long color to HTML hex color with alpha

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
User avatar
Evgeniy
Posts: 43
Joined: Thu Jan 09, 2020 9:31 pm
Location: Russia

BASIC Long color to HTML hex color with alpha

Post by Evgeniy »

Code: Select all

' Convert Long color value to HTML HEX color for example "#0ffa56"
' If used in HTML tag for example <FONT COLOR> tag use aditional ""
' If used in CSS STYLE for example background-color: not use "" 
Function LongToHTML_Hex(ColorValue as Long) As String
	LongToHTML_Hex="#"+Right("0"+Hex(red(ColorValue)),2)+Right("0"+Hex(green(ColorValue)),2)+Right("0"+Hex(blue(ColorValue)),2)
End Function

' With alpha channel support #RRGGBBAA
' For example #000000FF = black color with 100% transparency
Function LongToHTML_HexAlpha(ColorValue as Long) As String
	LongToHTML_HexAlpha="#"+Right("0"+Hex(red(ColorValue)),2)+Right("0"+Hex(green(ColorValue)),2)+Right("0"+Hex(blue(ColorValue)),2)+_
	Right("0"+Hex(red(ColorValue/256)),2)
End Function
How to use:

1) Simple message box

Code: Select all

msgbox "rgb(215,125,25)="+LongToHTML_Hex(rgb(215,125,25))+chr(10)+chr(10)+" HEX(DEADBEEF)="+LongToHTML_HexAlpha(-559038737)
2) HTML tag make

Code: Select all

...
color=LongToHTML_Hex(TextPortion.CharColor)
text=text+"<FONT COLOR="""+color+""">"+TextPortion.String+"</FONT>"
...
Last edited by Evgeniy on Sat Jan 11, 2020 10:25 pm, edited 2 times in total.
OpenOffice 4.1.7 OS: Win10 x32 + Win10 x64
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: BASIC Long color to HTML hex color with alpha

Post by JeJe »

LongToHTML_Hex(rgb(255,125,255)) gives #FF07FF when it should be #FF7DFF

VB6 code can often be adapted to OOBasic with no to few changes

https://codereview.stackexchange.com/qu ... ing-in-vb6
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Evgeniy
Posts: 43
Joined: Thu Jan 09, 2020 9:31 pm
Location: Russia

Re: BASIC Long color to HTML hex color with alpha

Post by Evgeniy »

Code: Select all

Sub Test
	Dim c As Long
	c=rgb(125,125,125)
	msgbox Format(Hex(blue(c)),"00")
End Sub
Yes thats true :( Format function is bugly?
Thanks for bug catch.
Right("0"+Hex(val), 2) interst basic trick!
Functions are Fixed.
OpenOffice 4.1.7 OS: Win10 x32 + Win10 x64
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: BASIC Long color to HTML hex color with alpha

Post by Villeroy »

What's wrong with ="#"&DEC2HEX(A1;6) :?:
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: BASIC Long color to HTML hex color with alpha

Post by JeJe »

result = "#000000": mid(result,8 - len(hex(no)))= hex(no)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
rickberon
Posts: 1
Joined: Wed Oct 21, 2020 8:10 am

Re: BASIC Long color to HTML hex color with alpha

Post by rickberon »

There is a simple Math behind it. RGB color code contains three numbers, each ranging from 0 to 255. Just convert these number into hexadecimal and put in sequence of #RGB. For Example, RGB(0,0,0) will be #000 and RGB(255, 255, 255) will be #FFFFFF.

How to convert from decimal to hex

Divide the number by 16.
Get the integer quotient for the next iteration.
Get the remainder for the hex digit.
Repeat the steps until the quotient is equal to 0.
OpenOffice 3.1 on Windows Vista
Post Reply