Thursday, April 17, 2008

mx:ColorPicker set color, convert uint to string of selectedColor

If you are working with <mx:ColorPicker /> in flex and assuming the following are your requirements:

a) You want to set the colorpicker to some color

b) working with selectedIndex, selectedColor

c) you want to store the selectedColor of the color picker as a String in your database and convert the stored String value to set it back to colorpicker so you can set the color to the color picker (<mx:colorpicker>), here you go:

Assuming you have two color pickers and you want to set the color picker value of 'cp' to 'newcp'

   <mx:ColorPicker id='cp'/>
   <mx:ColorPicker id='newcp' />

if you do {cp.selectedIndex} in your action script code, it will return you an int value which is equal to the index number of the palette. Every color has a index number, the default value=0.

    var cpSelectedIndexStr:String = cp.selectedIndex.toString();
    newcp.selectedIndex = int(cpSelectedIndexStr);

if you do {cp.selectedColor} in your action script code, it will return you a uint value. The following code will NOT work -

   var cpSelectedColorStr:String = cp.selectedColor.toString();
   newcp.selectedColor = uint(cpSelectedColorStr);

The following code will work -

   var cpSelectedColorStr:String = cp.selectedColor.valueOf().toString();
   newcp.selectedColor = uint(cpSelectedColorStr);

This is a very basic conversion funda.. hope you get it...