RGB to hex in flash

  • Started
  • Last post
  • 4 Responses
  • monNom

    I'm trying to set the color value (in hex) of a moviclip based on RGB values. I've got the Hex values coming out correctly from the RGB, but I think there is something wrong with how I'm printing the string because it's not being recognised by the .setRGB() function.

    here's the code, any ideas would be helpful... I've already tried eval().

    -----------------------------
    function swapCol(){
    var HexCharacters="0123456789ABCDEF...

    hexr = HexCharacters.charAt((r>>4)&0xf... value to hex

    hexg = HexCharacters.charAt((g>>4)&0xf... value to hex

    hexb = HexCharacters.charAt((b>>4)&0xf... value to hex

    hexCode = "0x"+hexr+hexg+hexb;

    checker = hexCode;//prints hex string to text box

    this.clr_blk = new Color(this.clr_blk);

    this.clr_blk.setRGB(hexCode);

    }
    --------------------------------

  • monNom0

    ummmmm... bump

  • billyFUN0

    try using parseint() to convert the string hex value to an integer.
    ie...

    foo.setRGB(parseint(var));

  • unknown0

    Well I'm not sure exactly what you're trying to achieve here..I mean I know but the code you wrote is a bit confusing.

    First of all when you use those setRGB assignments you first define the color object for your MC.

    What is 'this'? did you pass the object to the function? you have to pass the object as a function parameter and then use the name of that parameter when declaring color object for it.

    You are addressing the color model in a wrong way. Let me show you how I change the MCs color in one of my scripts:

    randomColor=RandomHelperColor(); // this is the function that generates random color from an array of preset colors and returns the picked one to randomColor var

    objectColor=new Color(_root.yourMC); // defines the color model for yourMC

    objectColor.setRGB(randomColor);

    that's it. Next I will include the randomColorPicker function so you can see what I did in there:

    _global.RandomHelperColor=functi...

    availableHelperColors=new Array("0x990000","0x999900","0xC... //
    randomColorPicker=random(availab...

    randomColorPicked=new String(availableHelperColors[ran...

    return randomColorPicked;
    }

    Hope this clarifies it a bit for you.

  • monNom0

    Thanks to both of you, that got me sorted out and on my way again.