cancel
Showing results for 
Search instead for 
Did you mean: 

Convert HTML Hex to RGB

10-24-2007 10:01 PM
1515 views 5 comments
0 Likes
SAP Managed Tags
Subscribe

Post Author: rtonerii

CA Forum: Formula

I have a client that is storing HTML Hexadecimal codes for all of there color codes. They want me to now display these in some reports. How can I do that? I can not find anything in CR XI R2 that lets me do that.

ThanksRick

0 Likes

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Likes

Post Author: V361

CA Forum: Formula

Thanks Rick, I copied it out and added to my handy dandy reference guide, (just in case some one else asks this question)

Former Member
0 Likes

Post Author: rtonerii

CA Forum: Formula

OK,

I wrote the function and it works like a charm.

Please post here if this was helpful to you. It just let's me know if posting this was worth the hassle.

I have 2 pieces in here. The first is how you can call it. NOTE I left the call in Crystal Syntax!

// Begin

StringVar HexColor ;

HexColor := {query.fieldname};

// In my field we require a format like #0000FF Start with the second characterRGB(HexToDec(Mid (HexColor,2 ,2)) , HexToDec(Mid (HexColor,4 ,2)) , HexToDec(Mid (HexColor,6 ,2)));

// End

Here is the function. NOTE I left the call in Basic Syntax!

Function HexToDec (Input AS String)

Dim a AS NumberDim b AS Number

Select Case Mid (Input,1 ,1) Case "A" a = 10 Case "B" a = 11 Case "C" a = 12 Case "D" a = 13 Case "E" a = 14 Case "F" a = 15 Case Else a = 0 End Select

Select Case Mid (Input,2 ,1) Case "A" b = 10 Case "B" b = 11 Case "C" b = 12 Case "D" b = 13 Case "E" b = 14 Case "F" b = 15 Case Else b = 0 End Select

HexToDec = (a * 16) + b

End Function

Happy ColoringRick

Former Member
0 Likes

There is slight bug in the above HexToDec function whereby the any Hexadecimal digit in the 1 to 9 range is always converted to a value of zero.

In order to correct this, I have made a change to the 'Case Else' condition that converts the value to the numeric equivalent of the digit instead (see below). This should correct an issue whereby some hex values were being incorrectly converted and resulting in black or white colours.

Thanks,

RichardF

Function HexToDec (Input AS String)

Dim a AS Number

Dim b AS Number

Select Case Mid (Input, 1, 1)

Case "A"

a = 10

Case "B"

a = 11

Case "C"

a = 12

Case "D"

a = 13

Case "E"

a = 14

Case "F"

a = 15

Case Else

a = Val(Mid(Input, 1, 1))

End Select

Select Case Mid (Input, 2, 1)

Case "A"

b = 10

Case "B"

b = 11

Case "C"

b = 12

Case "D"

b = 13

Case "E"

b = 14

Case "F"

b = 15

Case Else

b = Val(Mid(Input, 2, 1))

End Select

HexToDec = (a * 16) + b

End Function

Former Member
0 Likes

Post Author: rtonerii

CA Forum: Formula

I agree. I just have no idea how to get that number. Any ideas would be great.

ThanksRick

Former Member
0 Likes

Post Author: V361

CA Forum: Formula

You will need to convert the hex to a number between 0 and 255, then for your field (or section) you can use color (255,255,255) in this case the color is black, for example if I had parameters I could use color({?RedColor},{?GreenColor},{?BlueColor})

So, in your case, convert the hex to a number (one for red, green, blue) then put that result

into the Color statement color ({@red},{@green},{@blue})

crnocolor will reset the color should you need to.