【JavaScript】カラー

# RGBからHEXに変換

__概要__

RGBからHEXに変換

__施策__

```js
var rgb2Hex = function(rgb){

    function toHex(n) {
        n = parseInt(n,10);
        if (isNaN(n)) return "00";
        n = Math.max(0,Math.min(n,255));
        return "0123456789ABCDEF".charAt((n-n%16)/16)
            + "0123456789ABCDEF".charAt(n%16);
    }

    return '#'+toHex(rgb[0])+toHex(rgb[1])+toHex(rgb[2]);

};
```

# RGBからHSVに変換

__概要__

RGBからHSVに変換

__施策__


```js
var rgb2hsv = function(rgb) {

    var b, delta, g, h, r, s, v;
    r = Number(rgb[0]), g = Number(rgb[1]), b = Number(rgb[2]);

    var min = Math.min(r, g, b);
    var max = Math.max(r, g, b);
    delta = max - min;
    v = max / 255.0;
    if (max === 0) {
        h = Number.NaN;
        s = 0;
    } else {
        s = delta / max;
        if (r === max) {
            h = (g - b) / delta;
        }
        if (g === max) {
            h = 2 + (b - r) / delta;
        }
        if (b === max) {
            h = 4 + (r - g) / delta;
        }
        h *= 60;
        if (h < 0) {
            h += 360;
        }
    }

    if(isNaN(h)){
        h = 0;
    }

    return [h, s, v];

};
```

---

# HEXからRGBに変換

__概要__

HEXからRGBに変換

__施策__

```js
var hex2rgb = function(hex){
    function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
    function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
    function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
    function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}

    var r = Math.round(hexToR(hex));
    var g = Math.round(hexToG(hex));
    var b = Math.round(hexToB(hex));

    return [r,g,b];
};
```

---

# HEXからHSVに変換

__概要__

HEXからHSVに変換

__施策__

```js
var hex2hsv = function(hex){
    var rgb = hex2rgb(hex);
    var hsv = rgb2hsv(rgb);

    return hsv;
};
```

---

# HSVからRGBに変換

__概要__

HSVからRGBに変換

__施策__

```js
var hsv2rgb = function(hsv) {

    var b, f, g, h, i, p, q, r, ref, ref1, ref2, ref3, ref4, ref5, s, t, v;
    h = hsv[0], s = hsv[1], v = hsv[2];
    v *= 255;
    if (s === 0) {
        r = g = b = v;
    } else {
        if (h === 360) {
            h = 0;
        }
        if (h > 360) {
            h -= 360;
        }
        if (h < 0) {
            h += 360;
        }
        h /= 60;
        i = Math.floor(h);
        f = h - i;
        p = v * (1 - s);
        q = v * (1 - s * f);
        t = v * (1 - s * (1 - f));
        switch (i) {
            case 0:
                ref = [v, t, p], r = ref[0], g = ref[1], b = ref[2];
                break;
            case 1:
                ref1 = [q, v, p], r = ref1[0], g = ref1[1], b = ref1[2];
                break;
            case 2:
                ref2 = [p, v, t], r = ref2[0], g = ref2[1], b = ref2[2];
                break;
            case 3:
                ref3 = [p, q, v], r = ref3[0], g = ref3[1], b = ref3[2];
                break;
            case 4:
                ref4 = [t, p, v], r = ref4[0], g = ref4[1], b = ref4[2];
                break;
            case 5:
                ref5 = [v, p, q], r = ref5[0], g = ref5[1], b = ref5[2];
        }
    }
    r = Math.round(r);
    g = Math.round(g);
    b = Math.round(b);

    return [r, g, b];
};
```

# HSVからHEXに変換

__概要__

HSVからHEXに変換

__施策__

```js

var hsv2hex = function(hsv){
    var ths = this;

    var h = hsv[0];
    var s = hsv[1];
    var v = hsv[2];

    var rgb = hsv2rgb([h,s,v]);

    var hex = rgb2Hex([rgb[0],rgb[1],rgb[2]]);

    return hex;
};

```

---

# hexからcolorrefに変換

__概要__

hexからcolorrefに変換

__施策__

```js
var hex2colorref = function(hex){
    var stg = String(hex).replace(/#/g,'');
    var rr = stg.substr(4,2);
    var gg = stg.substr(2,2);
    var bb = stg.substr(0,2);

    var colorref = parseInt(rr+gg+bb,16);

    return colorref;
};
```
---

# colorrefからhexに変換

__概要__

colorrefからhexに変換

__施策__

```js
var colorref2hex = function(colorref){

    var st = '';
    var hex = Number(colorref).toString(16);

    var len = 6 - hex.length;
    for(var i=0; i<len; i++){
        st += '0';
    }

    var stg = st + hex;
    var rr = stg.substr(4,2);
    var gg = stg.substr(2,2);
    var bb = stg.substr(0,2);

    return '#' + rr + gg + bb;
};
```