View Full Version : HEX and RGB questions
JGizmo
03-26-2003, 11:07 PM
Hi guys,
I am trying to using RGB value (0-255) to change the textColor of a dynamic textField created by using createTextField.
maintf.textColor = 0xFFFFFF;//set the text to white first
now when I
trace(maintf.textColor>>16);//return 255
trace(maintf.textColor>>8);//return 65535
trace(maintf.textColor);//return 16777215
I wonder if anyone know how to convert the (65535 into 255)(1677215 to 255) or vice vesa?
Many thanks!
John
JGizmo
03-26-2003, 11:09 PM
I know other way to do this is to using setTransForm of an transformObject object type, but I dont want to use that.
Billy T
03-26-2003, 11:13 PM
open colour mixer
click on the stroke colour up the top left
in the panel that opens with all the swatches, click on the colour wheel up the top right
go into the colour sliders section of the panel that opens
make sure you are viewing RGB sliders
put in your RGB values
click ok
down the bottom left of your colour mixer you should have your hex number
cheers
senocular
03-27-2003, 01:05 AM
for hex to RGB you can use
r = hex >> 16
g = (hex ^ hex >> 16 << 16) >> 8
b = hex >> 8 << 8 ^ hex
which if all being done at once, can be reduced to
r = hex >> 16;
temp = hex ^ r << 16;
g = temp >> 8;
b = temp ^ g << 8;
going back to hex is simply
r << 16 ^ g << 8 ^ b
JGizmo
03-27-2003, 08:08 AM
Billy: I think you have misunderstood my question, and your answer should belongs to newbie section :D (joking) Thanks anyway Billy.
senocular: Thats just what I want, cheers for that!
John
Lynx75
03-27-2003, 09:14 AM
I give u an simple example to convert from RGB to HEX:
R=240;
G=200;
B=100;
mycolour=R.toString(16)+G.toString(16)+B.tostring( 16);
mynewcolours=parseint(mycolour,16);
trace(mynewcolours);
it should works.Let me know..
bye
Billy T
03-27-2003, 09:38 AM
Originally posted by JGizmo
I think you have misunderstood my question
yep I thought I might have... ;)
Lynx75
03-27-2003, 10:03 AM
ok,little explanation:
//set variables R,G,B as number
//u can even use ex. R=random(255)
R=240;
G=200;
B=100;
// so,we have 3 variables as number,but,are
//numbers,and to join them in order to convert
//we have to set them as string,so text!
//mycolour="240"+"200"+"100",so
//mycolous="240200100",so in string.
mycolour=R.toString(16)+G.toString(16)+B.tostring( 16);
//then we convert "240200100" into number
//and then into the 16hex numeric system.
mynewcolours=parseint(mycolour,16);
//well,u know what it does! 8)
trace(mynewcolours);
easy!
two really good books for learn actionscript are:
"Actionscript for Flash MX:the definitive guide" and "Flash Mx Actionscript - training from the source"..
really good ones...
bye
JGizmo
03-27-2003, 01:33 PM
I tired it but arnt work well....hmmmm
maintf.textColor = r << 16 ^ g << 8 ^ b;
/*
mycolour = r.toString(16)+g.toString(16)+b.tostring(16);
mynewcolours = parseint(mycolour, 16);
maintf.textColor = mynewcolours;
*/
The upper code works well but the bottom one which is using Lynx75's method arnt works well. (Still change color, but I guess the convertion not right coz I can see color jumped from green to blue between 2 integer lol, for the reference my range of interger is 0-255 (r,g,and b))
Would it be mycolour = r.toString(16)+g.toString(8)+b.tostring(0);
or something?
Cheers
John
senocular
03-27-2003, 02:15 PM
the reason r.toString(16)+g.toString(16)+b.tostring(16); wouldnt work is because of trailing 0's. For example, 255 in hex is FF. 15 is F. If you had a rgb of 255, 255, and 255, then, when converted and combined, you get FF + FF + FF == FFFFFF which when parsed is the correct hex. However, If you had a rgb of 15, 15, and 15, then, when combined you get F + F + F == FFF or equivalent to 000FFF and not 0F0F0F which would be the rgb of 15, 15 and 15.
now, if you're set on using that method, you could go in for each toString conversion and check if the length is less than 2, and, if so, add on that extra "0"
r2 = r.toString(16)
if (r2.length < 2) r2 = "0"+r2;
etc
carwash
12-14-2004, 04:12 PM
Ok, I have a script that changes masked colors on an area, and i have built it with hexidecimal colors. Also the colors are exported through a text field that is read by director.
Anyway, my client wants the values in the text field to read RGB and not hex. He wants a format like R201G120B024.
How can i do this?
Here is the current code...let me know if you have any insight into this.
on (release) {
c = new Color(_root.curcolbutton.curcolor);
picker = _root.picker;
if (picker.hitTest(_root._xmouse, _root._ymouse, false)) {
k = picker._xmouse;
m = picker._ymouse;
if ((k == oldK) && (m == oldm) && (cValue != 0x888888)) {
return;
}
gOffset = Math.abs(k-100);
gHue = picker.getHue(gOffset);
bOffset = Math.abs(k-200);
bHue = picker.getHue(bOffset);
if (k<100) {
rOffset = k;
} else {
rOffset = 300-k;
}
rHue = picker.getHue(rOffset);
// -----------------
// m = picker._ymouse;
// 50
getFadeHue = function (cHue, m) { var cFadeHue;if (m<=50) {cFadeHue = 255-Math.round((255-cHue)*m/50);} else {cFadeHue = Math.round((100-m)*cHue/50);}return cFadeHue;};
cValue = ((getFadeHue(rHue, m) << 16)+(getFadeHue(gHue, m) << 8)+getFadeHue(bHue, m));
} else {
cValue = 0x888888;
}
if (cValue != oldcValue) {
c.setRGB(cValue);
cString = "000000"+cValue.toString(16).toUpperCase();
cString = cString.substr(cString.length-6, 6);
_root.selcolor = cString;
_root.txt1stripe01="0x"+cString;
oldcValue = cValue;
}
}
"txt1strip01" is the variable of the text field that the characters are dumping to. thanks a lot.
jclarke
06-27-2007, 07:12 AM
I came across this post when I searched for "RGB to Hex in actionscript"
I tryed a couple of the posts and all of them worked but just not very well like it kept on coming up with a negative number.
But I worked it out and it was very simple: replace the "-" with a "0" (zero).
eg.
r = 255;
g = 200;
b = 120;
colour = new Color(myMC);
r2 = r.toString(16);
if (r2.length < 2) r2 = "0"+r2;
g2 = g.toString(16);
if (g2.length < 2) g2 = "0"+g2;
b2 = b.toString(16);
if (b2.length < 2) b2 = "0"+b2;
hex = r2+g2+b2;
hex = hex.split("-").join("0");
displayText.text = "#"+hex;
colour.setRGB("0x"+hex);
and that works 10/10 but just make sure that the r g b vars don't go below 0 or above 255 otherwise it stuffs up again.
my example is at http://lab.jacobclarkezone.com/lab/flash/colour%20spectrum.swf
(the actual site is at http://lab.jacobclarkezone.com/lab/)
-Jacob
varun.upadhyay
12-22-2008, 10:27 AM
for hex to RGB you can use
r = hex >> 16
g = (hex ^ hex >> 16 << 16) >> 8
b = hex >> 8 << 8 ^ hex
which if all being done at once, can be reduced to
r = hex >> 16;
temp = hex ^ r << 16;
g = temp >> 8;
b = temp ^ g << 8;
going back to hex is simply
r << 16 ^ g << 8 ^ b
Thanks a lot senocular, Its gr8 work by you.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.