I use this snippet to convert RGB color to CMYK in javascript:
function RgbToCmyk(R,G,B)
{
if ((R == 0) && (G == 0) && (B == 0)) {
return [0, 0, 0, 1];
} else {
var calcR = 1 - (R / 255),
calcG = 1 - (G / 255),
calcB = 1 - (B / 255);
var K = Math.min(calcR, Math.min(calcG, calcB)),
C = (calcR - K) / (1 - K),
M = (calcG - K) / (1 - K),
Y = (calcB - K) / (1 - K);
return [C, M, Y, K];
}
}
now I want to convert returned CMYK to percentage CMYK.
for example this RGB color (171,215,170) bee converted to this percentage CMYK (34%, 1%, 42%, 0)
(I used photoshop for converting)
EDIT: returned values of this snippet is between 0-1 . I found that I must change this snippet to returns values between 0-255 and then divided values by 2.55 to give me values of cmyk color as percentage. now how change this code to return values in range of 0-255 ??
I use this snippet to convert RGB color to CMYK in javascript:
function RgbToCmyk(R,G,B)
{
if ((R == 0) && (G == 0) && (B == 0)) {
return [0, 0, 0, 1];
} else {
var calcR = 1 - (R / 255),
calcG = 1 - (G / 255),
calcB = 1 - (B / 255);
var K = Math.min(calcR, Math.min(calcG, calcB)),
C = (calcR - K) / (1 - K),
M = (calcG - K) / (1 - K),
Y = (calcB - K) / (1 - K);
return [C, M, Y, K];
}
}
now I want to convert returned CMYK to percentage CMYK.
for example this RGB color (171,215,170) bee converted to this percentage CMYK (34%, 1%, 42%, 0)
(I used photoshop for converting)
EDIT: returned values of this snippet is between 0-1 . I found that I must change this snippet to returns values between 0-255 and then divided values by 2.55 to give me values of cmyk color as percentage. now how change this code to return values in range of 0-255 ??
Share Improve this question edited Apr 8, 2014 at 18:13 asked Apr 8, 2014 at 13:41 user2057190user2057190 2- I don't see how 171, 215, 170 transforms to 34%, 1%, 42%, 0 – thefourtheye Commented Apr 8, 2014 at 14:06
- I dont know too. I used photoshop to convert rgb to percentage cmyk !! – user2057190 Commented Apr 8, 2014 at 14:14
2 Answers
Reset to default 2CMYK and RGB are two different colour models (subtractive and additive models respectively), hence you need to have an algorithm to convert the values (to get closest equivalent of each system in the other one)
I'll suggest you to have a look here:
RGB-to-CMYK Color Conversion
using an algorithm like that you should be able to convert the values back and forth and then to get the percentage you just need to do a simple calculation, for example this function will return a percentage value of a given system:
function get_percentage(value, model){
if (model == "CMYK") return value; // CMYK values are 0-100 (if 0-1 just * 100)
if (model == "RGB" ) return (value/ 255)* 100;
}
// a call with the value of 66 in RGB model:
document.write( get_percentage( 66, "RGB"));
Hope it helps a bit,
I use this function:
function rgb2cmyk([rgb, a]: [number[], number]): [number[], number] {
const r = rgb[0] / 255;
const g = rgb[1] / 255;
const b = rgb[2] / 255;
const k = +(1 - Math.max(r, g, b)).toFixed(1);
const c = +((1 - r - k) / (1 - k) || 0).toFixed(1);
const m = +((1 - g - k) / (1 - k) || 0).toFixed(1);
const y = +((1 - b - k) / (1 - k) || 0).toFixed(1);
return [[c * 100, m * 100, y * 100, k * 100], a];
}