Question

Hey all, I have this assignment where I need to integrate the Yahoo UI color picker into a website. When someone picks a color the background of the page need to become that color.

I followed instructions here http://developer.yahoo.com/yui/colorpicker/#events but I can't seem to figure out how to change the backgroundcolor to the picked color. I can change the background when someone picks a color, however I don't know how to get the color that is picked as input/ (see code for // comment) up until now i have this:

in head:

<!-- Dependencies --> 
<script src="http://yui.yahooapis.com/2.8.2r1/build/utilities/utilities.js" ></script>
<script src="http://yui.yahooapis.com/2.8.2r1/build/slider/slider-min.js" ></script>

  <!-- Color Picker source files for CSS and JavaScript -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.2r1/build/colorpicker/assets/skins/sam/colorpicker.css">
<script src="http://yui.yahooapis.com/2.8.2r1/build/colorpicker/colorpicker-min.js" ></script>

then in body tag: class="yui-skin-sam"

and in body:

<div id="container"></div>
<script type="text/javascript">
var picker = new YAHOO.widget.ColorPicker("container", {
 showhsvcontrols: true,
 showhexcontrols: true,
 images: {
  PICKER_THUMB: "picker_thumb.png",
  HUE_THUMB: "hue_thumb.png"
 }
});

//a listener for logging RGB color changes;
//this will only be visible if logger is enabled:
var onRgbChange = function(o) {
 /*o is an object
  { newValue: (array of R, G, B values),
    prevValue: (array of R, G, B values),
    type: "rgbChange"
   }
 */
    // with this code i change the background when a color is picked, but not with the input col.
    // document.body.style.backgroundColor = 'green';
 YAHOO.log("The new color value is " + o.newValue, "info", "example");
}

//subscribe to the rgbChange event;
picker.on("rgbChange", onRgbChange);

  </script>
Was it helpful?

Solution

The syntax for setting the background color to a RGB value (what o.newValue gives) looks like:

doc.style.backgroundColor="rgb(239,239,239)";

...so, try something like this:

document.body.style.backgroundColor = "rgb(" + o.newValue[0] + "," + o.newValue[1] + "," + o.newValue[2] + ");"  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top