Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting the Hexa values to color(red,green,blue) ie., RGB format

Status
Not open for further replies.

raj0927

Programmer
Oct 8, 2010
26
US
Hi,
From the front end the colors are passed in the Hexa decimal format i have to convert that into (red,gree,blue) format in crystal reports and color my feild based on that.is there any function to do that. I am cr2008
 
i think you would need to extract the individual color characters and then convert them to decimal values.

for example: #7CFC00 would convert as:
7C = (7*16)+12 = 112+12 = 124
FC = (15*16)+12 = 240+12 = 252
00 = (0*16)+0 = 0+0 = 0
for a RGB value of (124,252,0)

 
How can i do this in crystal reports. Is there any function or should i use variables.Can you please guide me.
 
i don't know of any function.
i think it would have to done via brute force using formulas.
Perhaps one for each color value.
Since Hex color values start with '#', you have to take that into account, something like:

stringvar rd := MID({table.field},2,1);
stringvar rd2 := MID({table.field},3,1);
stringvar grn := MID({table.field},4,1);
stringvar grn2 := MID({table.field},5,2);
stringvar blu := MID({table.field},6.1);
stringvar blu2 := RIGHT({table.field},1);

then evaluate each variable for a numeric or alpha then if needed, convert alpha to numeric (A=10, B=11, etc), then perform the math to get your end results.


 
I can you please provide me what are the numeric values for the respective alpha values
 
here is are list and explaination:

A=10
B=11
C=12
D=13
E=14
F=15


The value actually changes based on the position.
for example, all hex colors only use 2 characters.
in base10 that would be 10's and 1's.
in base16 (hex) it is 16's and 1's.
for example, "A3" would be the quantity 10 times 16 plus the quantity 3 times 1 or, 163

the links above should provide you the information necessary to create conversions.
 

I thought this was interesting and I had the time so here is a formula that you can use, or it would probably be better to convert to a custom function:

Code:
whileprintingrecords;
stringvar v_hexvalue;
numbervar x;
numbervar y;
numbervar z:= 1;
numbervar a := 0;
local stringvar array v_rgb;

v_hexvalue := replace({YourHexField},"#","");

while a < 3

do

(if isnumeric(v_hexvalue[z]) then x := tonumber(v_hexvalue[z]) else 
if v_hexvalue[z] = "A" then x := 10;
if v_hexvalue[z] = "B" then x := 11;
if v_hexvalue[z] = "C" then x := 12;
if v_hexvalue[z] = "D" then x := 13;
if v_hexvalue[z] = "E" then x := 14;
if v_hexvalue[z] = "F" then x := 15;

if isnumeric(v_hexvalue[z+1]) then y := tonumber(v_hexvalue[z+1]) else 
if v_hexvalue[z+1] = "A" then y := 10;
if v_hexvalue[z+1] = "B" then y := 11;
if v_hexvalue[z+1] = "C" then y := 12;
if v_hexvalue[z+1] = "D" then y := 13;
if v_hexvalue[z+1] = "E" then y := 14;
if v_hexvalue[z+1] = "F" then y := 15;

redim preserve v_rgb[a + 1];

v_rgb[a+1] := totext(((x * 16) + y),"#",0);

a := a + 1;
z := z + 2);


"(" + join(v_rgb,",") + ")"

Note that this returns a text string - not sure if CR can take that as an RGB value since it would normally be three numbers contained withing parentheses.

Fisherromacse did the hard work on this one.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top