A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: retrieving R, G and B values from colour?

  1. #1
    Senior Member
    Join Date
    Mar 2010
    Posts
    157

    Question 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

  2. #2
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    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(
    == null) { 
            return 
    "00";
        }
        
    parseInt(N,10); 
        if (
    == || isNaN(N)) {
            return 
    "00";
        }
        
    Math.max(0,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?"

  3. #3
    Senior Member
    Join Date
    Mar 2010
    Posts
    157
    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;

  4. #4
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    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 
    clr >> 16;
        var 
    temp clr << 16;
        var 
    temp >> 8;
        var 
    temp << 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?"

  5. #5
    Senior Member
    Join Date
    Mar 2010
    Posts
    157
    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!

  6. #6
    Senior Member
    Join Date
    Mar 2010
    Posts
    157
    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!

  7. #7
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    Glad I could help.
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center