BASIC Long color to HTML hex color with alpha

Creating Extension - 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 forum is not for asking questions about writing your own macros.

BASIC Long color to HTML hex color with alpha

Postby Evgeniy » Sat Jan 11, 2020 4:21 pm

Code: Select all   Expand viewCollapse view
' 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   Expand viewCollapse view
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   Expand viewCollapse view
...
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
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

Postby JeJe » Sat Jan 11, 2020 9:52 pm

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
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1251
Joined: Wed Mar 09, 2016 2:40 pm

Re: BASIC Long color to HTML hex color with alpha

Postby Evgeniy » Sat Jan 11, 2020 10:05 pm

Code: Select all   Expand viewCollapse view
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
Evgeniy
 
Posts: 43
Joined: Thu Jan 09, 2020 9:31 pm
Location: Russia

Re: BASIC Long color to HTML hex color with alpha

Postby Villeroy » Sun Jan 12, 2020 1:09 am

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, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28853
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: BASIC Long color to HTML hex color with alpha

Postby JeJe » Sun Jan 12, 2020 11:42 am

result = "#000000": mid(result,8 - len(hex(no)))= hex(no)
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1251
Joined: Wed Mar 09, 2016 2:40 pm

Re: BASIC Long color to HTML hex color with alpha

Postby rickberon » Wed Oct 21, 2020 8:13 am

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
rickberon
 
Posts: 1
Joined: Wed Oct 21, 2020 8:10 am


Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 2 guests