问题
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) become 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 ??
回答1:
CMYK 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,
来源:https://stackoverflow.com/questions/22938896/how-convert-rgb-or-cmyk-color-to-percentage-cmyk-javascript