1

I'm working on a web application in which I'm using javascript color picker to pick a color. It is giving me color in form of HSV and i want to convert it into hex form.Can anyone help so that i pass HSV to a function which will return color in form of HEX. Here is my code: I'm using a function of spectrum.js, which is not giving the proper output.

<script src="scripts/spectrum.js"></script>
$("#showInputWithClear").spectrum({
    showInput: true,
});

Thanks in advance...

Ashish Gupta
  • 23
  • 1
  • 3
  • so what output are you getting from `spectrum.js` ? – Alnitak Apr 09 '16 at 14:51
  • [HSV to RGB conversion formula](http://www.rapidtables.com/convert/color/hsv-to-rgb.htm), and similar (but not the same) question was on StackOverflow, [conversion of HSL to RGB](http://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion) – Farside Apr 09 '16 at 15:05

2 Answers2

3

You can use https://github.com/Automattic/Color.js to get the color. Try the below code I hope, It'll help you.

<script src="https://github.com/Automattic/Color.js"></script>
function GetColorCode() {
     var hsv = $("#showInputWithClear").val();
     var c = Color( hsv );
     //  alert(hsv);
     c.toString();
    // "#0c2291"
}
keshav vishwkarma
  • 1,744
  • 11
  • 20
3

Looks like spectrum.js has an option, preferredFormat, which will give you a hex code directly, instead of using an additional library. Here's an example:

$("#showInputWithClear").spectrum({
    preferredFormat: "hex",
    showInput: true,
});
Jay Harris
  • 95
  • 9