[Solved] How do I convert RGB to value range 0.0-1.0?

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
sbaden
Posts: 35
Joined: June 16th, 2009, 1:07 pm
Location: Santa Clarita, CA

I can't seem to find reference for converting RGB values to the value range that javascript needs [0.0-1.0].

Any help would be greatly appreciated. Thanks.
Last edited by sbaden on December 29th, 2009, 4:21 pm, edited 1 time in total.
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

If the values are 8 bit 0-255 you would just divide each rgb value by 255.
sbaden
Posts: 35
Joined: June 16th, 2009, 1:07 pm
Location: Santa Clarita, CA

Thank you Paul :D

Do you have a reference for converting Hex to RGB?
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

You can use parsetInt(yourHexString, 16) to convert from hex to decimal.
sbaden
Posts: 35
Joined: June 16th, 2009, 1:07 pm
Location: Santa Clarita, CA

LMAO! :D Well, I guess that would be a much simpler, more direct way of getting HEX to decimal instead of converting HEX to RGB and then converting RGB to decimal.

I will go try this out...

Thank you
sbaden
Posts: 35
Joined: June 16th, 2009, 1:07 pm
Location: Santa Clarita, CA

Okay, I'm a little confused still...

How would the parseInt() work in this scenario?

Code: Select all

//Convert HEX to RGB_______________________________________________________________________________________________

		function GiveDec(Hex)
		{
			if(Hex == "A")
				Value = 10;
			else
			if(Hex == "B")
				Value = 11;
			else
			if(Hex == "C")
				Value = 12;
			else
			if(Hex == "D")
				Value = 13;
			else
			if(Hex == "E")
				Value = 14;
			else
			if(Hex == "F")
				Value = 15;
			else
				Value = eval(Hex);

			return Value;
		}


 		function HexToDec()
 		{

			lastNameColor = lastNameColor.toUpperCase(); //change all lowercase letters to uppercase

			var a = GiveDec(lastNameColor.substring(0, 1));
			var b = GiveDec(lastNameColor.substring(1, 2));
			var c = GiveDec(lastNameColor.substring(2, 3));
			var d = GiveDec(lastNameColor.substring(3, 4));
			var e = GiveDec(lastNameColor.substring(4, 5));
			var f = GiveDec(lastNameColor.substring(5, 6));

			redValue = (a * 16) + b;
			greenValue = (c * 16) + d;
			blueValue = (e * 16) + f;
		   
		   //convert RGB values to decimal values
			var redDecimal = (redValue/2.55)/100;
			var greenDecimal = (greenValue/2.55)/100;
			var blueDecimal = (blueValue/2.55)/100;

		}	
		//__________________________________________________________________________________________________________

              //Set color for last name (Animator on text layer: Fill Color)
		var colorLastName= lastNameLayer.property("Text").property("Animators").property("Animator 1").property("Properties").property("Fill Color");
		colorLastName.setValue([redDecimal, greenDecimal, blueDecimal, 1]);
	     //____________________________________________________________________________________________________

Dan Ebberts
Posts: 320
Joined: June 26th, 2004, 10:01 am
Location: Folsom, CA
Contact:

I think all you need is something like this:

redSubstr = lastNameColor.substr(0,2);
greenSubstr = lastNameColor.substr(2,2);
blueSubstr = lastNameColor.substr(4,2);

redDecimal = parseInt(redSubstr,16)/255;
greenDecimal = parseInt(greenSubstr,16)/255;
blueDecimal = parseInt(blueSubstr,16)/255;



Dan
sbaden
Posts: 35
Joined: June 16th, 2009, 1:07 pm
Location: Santa Clarita, CA

Thanks Dan,

Brilliant! It works great!

That is so much less code than I had.
Post Reply