如何将RGB或CMYK颜色转换为CMYK百分比-javascript [英] how convert RGB or CMYK color to percentage CMYK - javascript

查看:805
本文介绍了如何将RGB或CMYK颜色转换为CMYK百分比-javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码段在javascript中将RGB颜色转换为CMYK:

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];
    }
}

现在我想将返回的CMYK转换为百分比CMYK.

now I want to convert returned CMYK to percentage CMYK.

例如,此RGB颜色(171,215,170)转换为该百分比的CMYK(34%,1%,42%,0)

for example this RGB color (171,215,170) become converted to this percentage CMYK (34%, 1%, 42%, 0)

(我使用photoshop进行转换)

(I used photoshop for converting)

编辑:此代码段的返回值介于0-1之间.我发现我必须更改此代码段,以返回0-255之间的值,然后将值除以2.55,以得到百分比的cmyk颜色值.现在如何更改此代码以返回0-255范围内的值?

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 ??

推荐答案

CMYK和RGB是两种不同的颜色模型(分别是减法模型和加性模型),因此您需要一种算法来转换值(以获得与每个系统都在另一个系统中)

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)

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"));

希望这会有所帮助,

这篇关于如何将RGB或CMYK颜色转换为CMYK百分比-javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆