[SOLVED] Hex values from a string

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

[SOLVED] Hex values from a string

Post by Chris Corbyn »

I have this string

#f700e2

or something similar (a hex color code)

How do I use javascript to get the red part (f7), the green part (00) and the blue part (e2) as hexadecimal values?

I tried

Code: Select all

var redhex = 0xhexstring.substr(1,2) //Thought it would give 0xf7
But it didn't work... Otherwise how can I convert the string "f7" obtained using substr() into it's decimal value?

Cheers
Last edited by Chris Corbyn on Mon Feb 07, 2005 5:12 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

substring() is a string based method in Javascript.

Code: Select all

var hex = '7F2F0F';
var red = hex.substring(0,2); // or something like that..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah I new that so what I did was I used substr to get the hex pair then parseint(hexvalue,16) to make the hex in the end.

Thanks :-)
Post Reply