简体   繁体   English

如何将RGB或CMYK颜色转换为CMYK百分比-javascript

[英]how convert RGB or CMYK color to percentage CMYK - javascript

I use this snippet to convert RGB color to CMYK in javascript: 我使用此代码段将javascript的RGB颜色转换为CMYK:

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. 现在我想将返回的CMYK转换为百分比CMYK。

for example this RGB color (171,215,170) become converted to this percentage CMYK (34%, 1%, 42%, 0) 例如,此RGB颜色(171,215,170)转换为该百分比CMYK(34%,1%,42%,0)

(I used photoshop for converting) (我使用photoshop进行转换)

EDIT: returned values of this snippet is between 0-1 . 编辑:此代码段的返回值在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. 我发现我必须更改此代码段以返回0-255之间的值,然后将值除以2.55,以得到百分比的cmyk颜色值。 now how change this code to return values in range of 0-255 ?? 现在如何更改此代码以返回0-255范围内的值?

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) CMYK和RGB是两种不同的颜色模型(分别为减法模型和加法模型),因此您需要一种算法来转换值(以获取与另一系统中每个系统最接近的等效值)

I'll suggest you to have a look here: 我建议您在这里看看:

RGB-to-CMYK Color Conversion RGB到CMYK颜色转换

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, 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM