How to convert RGB value to CMYK in javascript

0 votes
34 views
asked May 27, 2016 in java script by jonathan (3,105 points)
HI All

    Please provide the best way to convert the RGB value to CMYK value in java script.

Thanks in advance

1 Answer

0 votes
answered May 27, 2016 by admin (8,115 points)
function RGBtoCMYK(r,g,b){
        var result = new CMYK(0, 0, 0, 0);
        r = r / 255;
        g = g / 255;
        b = b / 255;
        result.k = Math.min( 1 - r, 1 - g, 1 - b );
        result.c = ( 1 - r - result.k ) / ( 1 - result.k );
        result.m = ( 1 - g - result.k ) / ( 1 - result.k );
        result.y = ( 1 - b - result.k ) / ( 1 - result.k );
        result.c = Math.round( result.c * 100 );
        result.m = Math.round( result.m * 100 );
        result.y = Math.round( result.y * 100 );
        result.k = Math.round( result.k * 100 );
        return result;
    }

    function CMYK(c, m, y, k) {
        if (c <= 0) { c = 0; }
        if (m <= 0) { m = 0; }
        if (y <= 0) { y = 0; }
        if (k <= 0) { k = 0; }
        if (c > 100) { c = 100; }
        if (m > 100) { m = 100; }
        if (y > 100) { y = 100; }
        if (k > 100) { k = 100; }
        this.c = c;
        this.m = m;
        this.y = y;
        this.k = k;
    }
...