PDA

View Full Version : colorpicker, setting initial color



willsmelser
07-28-2008, 06:09 PM
I am having one heck of a time trying to figure out how to set the initial color for the ddcolorpicker.

Im sure it shouldnt be that difficult. I get the preview to show the correct color no problem, but when the dhtml window is opened, the initial color is set to #FFFFFF instead of my input value.

Any help would be great!

I looked on yahoo yui and there is a setValue function, but I cannot seem to call it correctly. niether ddcolorpicker.setValue or colorpicker.setValue seem to work.

Thanks,
Will

phollows
01-23-2009, 08:48 PM
to set or initialize the YUI color from a value in your form you need to make the following changes to ddcolorpicker.js:

in the "definepicker" method, make eadch use of the "colorpicker" variable to "this.colorpicker" - this makes the YUI colorpicker object a member of the ddcolorpicker class.

You can then reference it and initialize it as follows. In the init:function method definition look for the code "this.relatedinput.focus()" in the "config.colorfields[i].control.onclick=function(e)" definition.

After the "this.relatedinput.focus()" line, insert the following:

if(ddcolorpicker.validhexcheck(this.relatedinput.value))
{
var c=this.relatedinput.value
c=c.replace(/^#/, '')
var a=new Array ;
a=YAHOO.util.Color.hex2rgb(c)
ddcolorpicker.colorpicker.setValue(a, true);
} else {
ddcolorpicker.colorpicker.setValue([255,255,255], true);
}

What it does is this: If there's a valid hex color code it uses the yahoo utilities to convert the hex code (stripped of the leading '#') to an RGB array. Then the YUI color picker is initalized to that color. If not, the control is set to white (you can change this, obv.).

This works because we made colorpicker a member of the class with the first change at the top.

This drove me nuts for a couple of hours today - hope this helps.