Search code examples
javascriptjquerycsscolorscolor-picker

How to implement a color picker instead of static colors


I am developing a painting website. I have a script for selecting colors. It is working fine till now.

Here is the code to selecting my colors.

<div class="colorpick">
  <div class="pick" style="background-color:rgb(150, 0, 0);" onclick="hello(this.style.backgroundColor);"></div>
  <div class="pick" style="background-color:rgb(0, 0, 152);" onclick="hello(this.style.backgroundColor);"></div>
  <div class="pick" style="background-color:rgb(0, 151, 0);" onclick="hello(this.style.backgroundColor);"></div>
  <div class="pick" style="background-color:rgb(255, 0, 5);" onclick="hello(this.style.backgroundColor);"></div>
  <div class="pick" style="background-color:rgb(255, 255, 0);" onclick="hello(this.style.backgroundColor);"></div>
  <div class="pick" style="background-color:rgb(0, 255, 255);" onclick="hello(this.style.backgroundColor);"></div>
  <div class="pick" style="background-color:rgb(255, 0, 255);" onclick="hello(this.style.backgroundColor);"></div>
  <div class="pick" style="background-color:rgb(255, 150, 0);" onclick="hello(this.style.backgroundColor);"></div>
  <div class="pick" style="background-color:rgb(255, 0, 150);" onclick="hello(this.style.backgroundColor);"></div>
  <div class="pick" style="background-color:rgb(0, 255, 150);" onclick="hello(this.style.backgroundColor);"></div>
  <div class="pick" style="background-color:rgb(150, 0, 255);" onclick="hello(this.style.backgroundColor);"></div>
  <div class="pick" style="background-color:rgb(0, 150, 255);" onclick="hello(this.style.backgroundColor);"></div>
</div>

The colors are shown as

enter image description here

This is the code to my function hello()

<script>
  function hello(e){
    var rgb = e.replace(/^(rgb|rgba)\(/,'').replace(/\)$/,'').replace(/\s/g,'').split(',');
    myColor.r = parseInt(rgb[0]);
    myColor.g = parseInt(rgb[1]);
    myColor.b = parseInt(rgb[2]);
    curColor = myColor;
    console.log(curColor);
  }
</script>

But I want to implement a color picker. I have the script for color picker as well.

<input type="color" value="#ffffff" onchange="clickColor(0, -1, -1, 5)" id="html5colorpicker">

MY QUESTION IS THAT: How can I integrate color picker with my current code? Any help would be much appreciated.


Solution

  • Try this : You can use spectrum.js along with jquery ui for implementing colorpicker.

    HTML:

    <h2>Basic Usage</h2>
    <input type='text' class="basic"/>
    

    jQuery:

    $(".basic").spectrum({
        color: "#f00",
        change: function(color) {
            $("#basic-log").text("change called: " + color.toHexString());
        }
    });
    

    JSFiddle Demo

    http://bgrins.github.io/spectrum/spectrum.js
    http://bgrins.github.io/spectrum/spectrum.css