Hex Colour

Hello there,

I’m having a bit of trouble converting a Hex colour for use in juce. I receive the colour RGB data as a hex value (String), and cannot change the way I receive this, and want to create a new Juce::Colour(R, G, B) (no alpha).

I’ve had a browse on the forum and internet, and found this site… http://www.javascripter.net/faq/hextorgb.htm

I can’t for the life of me figure out how to do this in juce. I seem to have a mental block on how to parse the hex value to an int value. Can someone give me a helpful hint in the right direction?

From the site in JavaScript…

[code]R = hexToR("#FFFFFF");
G = hexToG("#FFFFFF");
B = hexToB("#FFFFFF");

function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}[/code]

Colour::fromString() ?

I think the Colour class will do this automatically for you without having to worry about all the String stuff. Try Colour::fromString. It internally uses String::getHexValue32 which seems to intelligently deal with case and hashes etc.

Thanks for the quick replies.

I’ve tried that function, but seem to be getting weird results. I receive the colour as “FF0000” for red for example, but it seems Colour::fromString needs alpha parameter, otherwise I just get plain white. I’ve tried the code below, both appending and prepending the “FF”, but with unexpected colours. Am I missing something silly?

String colourAsString = labelData[i]->getLabelColour(); colourAsString += "FF"; Colour newColourToDisplay = Colour::fromString(colourAsString);

I’ve also tried Colour::fromString("#FF0000") but this doesn’t produce a colour either. I’m using the latest from the tip.

But that code isn’t prepending FF, its appending it. Try something like:

const String colourString ("225566"); const Colour colour (Colour::fromString ("FF" + colourString));

Thanks. I did try it both ways.

It turns out the code was working right. After a bit of faffing in photoshop, it turns out the data sent to me is utter garbage. The colours that should be produced are not the same as the Hex values. What a joke.

It’s been a long day. Apologies and thanks again.