|
-
retrieving R, G and B values from colour?
Hey all!
I have a game, in which the player can open maps to edit them. The maps each have their own colour.
The player assigns a colour to the map by using sliders (R, G, B)
This all works fine.
However, I also need a way to put those sliders back in place when re-opening an existing map. This means that I need to know what the percentages of R, G and B are in a colour.
I made flash trace a few of those colours, this is the output:
16646398
0
16646144
65024
58878
Can anybody help me here? I need to know the amounts of R, G and B in any colour.
Thanks in advance
-
Senior Member
This should help, at least somewhat. I found some JS code that converted RGB to hex and just changed it to be used with AS2. Not too sure how your file is set up, but mine was setup with 3 input fields(r, g & b) and 1 dynamic text field "hex". HTH.
PHP Code:
function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) { if(N == null) { return "00"; } N = parseInt(N,10); if (N == 0 || isNaN(N)) { return "00"; } N = Math.max(0,N); N = Math.min(N,255); return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16); }
btn.onRelease = function() { hex.text = "0x"+RGBtoHex(Number(r.text), Number(g.text), Number(b.text)); }
Wile E. Coyote - "Clear as mud?"
-
That's a nice bit of code. However, it uses three input values. All I have is a Number, which represents the colour.
My interface uses three colour sliders only. I need to reset those sliders to be on the right values.
In other words, if the colour is orange (the hey would be 0xFF8800), I want my red slider to be on 100%, my green slider to ben on 50% and my blue one to be on 0%
By the way, I also use a funciton similar to yours, but it goes:
Actionscript Code:
r = Math.round(red_btn._x); g = Math.round(green_btn._x); b = Math.round(blue_btn._x); myColour = r*2.55<<16|g*2.55<<8|b*2.55;
-
Senior Member
Sorry for the lagged response... took some time to figure this one out, a bit more difficult than going from rgb to hex. However, to go from hex to rgb try using something like this. I used numeric stepper components just to show separate values.
PHP Code:
test.text = "0x111213"; // fills input textfield with just a hex value var clr = Number(test.text);
function convert() { clr = Number(test.text); var r = clr >> 16; var temp = clr ^ r << 16; var g = temp >> 8; var b = temp ^ g << 8; test.text = "0x"; rd.value = r; //numeric stepper to show r value gr.value = g; // same to show g value bl.value = b; // same to show b value }
btn.onRelease = function() { convert(); }
HTH.
Wile E. Coyote - "Clear as mud?"
-
I haven't got the time to test it on short notice, but this seems to be exactly what I'm looking for. Thank you very much 
Interesting to see this:
Actionscript Code:
var r = clr >> 16; var temp = clr ^ r << 16; var g = temp >> 8; var b = temp ^ g << 8;
It looks strange.
However, to be honest, I'm not sure what the >> operator does. I'll have to look into it in a bit, because it seems to be rather useful.
Again, thank you 
Cheers!
-
So yes, I tested the code and it worked!
I used this:
Actionscript Code:
function getRGB(myColour:String){ var colour:Number = Number(myColour); var R = colour >> 16; var temp = colour ^ R << 16; var G = temp >> 8; var B = temp ^ G << 8;ww trace(R+", "+G+", "+B); menu_mc.red_btn._x = R/254*100; menu_mc.green_btn._x = G/254*100; menu_mc.blue_btn._x = B/254*100; }
Thanks again!
-
Senior Member
Wile E. Coyote - "Clear as mud?"
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|