将 RGB 颜色转换为 CMYK 颜色?

发布于 2024-08-25 12:17:53 字数 124 浏览 7 评论 0原文

我正在寻找一种将 RGB 颜色转换为 CMYK 颜色的算法。 Photoshop 正在执行以下转换:

R = 220 G = 233 B = 174

C = 15 中号=0 Y = 40 K = 0

I'm looking for an algorithm to convert an RGB color to CMYK. Photoshop is performing the conversion below:

R = 220
G = 233
B = 174

C = 15
M = 0
Y = 40
K = 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

诗笺 2024-09-01 12:17:53

从 RGB 到 CMYK 的转换取决于用于铺设 CMYK 墨水的物理设备/过程。这些在软件中表示为颜色配置文件。 ICCICM 物理设备的颜色配置文件决定最终的颜色。

如果您不关心物理设备上的真实表示,请使用其他帖子中的直接转换公式。

但是,如果您担心这方面,那么您需要使用 Windows 颜色管理 API 或类似 LittleCMS 为您执行颜色转换(因为它们应用转换期间正确的颜色配置文件)。

The conversion from RGB to CMYK is dependent on the physical device/process being used to lay down the CMYK ink. These are represented in software as Color Profiles. ICC and ICM color profiles of physical devices determine the resulting colors.

If you are not concerned with true representation on a physical device then use the direct conversion formulas in other posts.

If, however, you are concerned with this aspect, then you need to use a either the Windows Color Management APIs or something like LittleCMS to do the color conversions for you (as they apply the proper color profile during the conversion).

新人笑 2024-09-01 12:17:53

查看此链接:http://www.codeproject.com/KB/applications/xcmyk。 .aspx.它给出了这个公式。

Black   = minimum(1-Red,1-Green,1-Blue)
Cyan    = (1-Red-Black)/(1-Black)
Magenta = (1-Green-Black)/(1-Black)
Yellow  = (1-Blue-Black)/(1-Black) 

Check out this link: http://www.codeproject.com/KB/applications/xcmyk.aspx. It gives this formula.

Black   = minimum(1-Red,1-Green,1-Blue)
Cyan    = (1-Red-Black)/(1-Black)
Magenta = (1-Green-Black)/(1-Black)
Yellow  = (1-Blue-Black)/(1-Black) 
桜花祭 2024-09-01 12:17:53

如果您想要好的结果,您需要应用颜色配置文件。在 .NET 中,您可以这样做(假设原始 CMYK 分量在 0 到 255 之间的范围内):

float[] colorValues = new float[4];
colorValues[0] = c / 255f;
colorValues[1] = m / 255f;
colorValues[2] = y / 255f;
colorValues[3] = k / 255f;

System.Windows.Media.Color color = Color.FromValues(colorValues,
    new Uri(@"C:\Users\me\Documents\ISOcoated_v2_300_eci.icc"));
System.Drawing.Color rgbColor = System.Drawing.Color.FromArgb(color.R, color.G, color.B);

请注意,使用了来自两个不同命名空间的两个不同颜色类。您可能需要添加PresentationCore DLL 作为参考。

所需的颜色配置文件可以从 eci.org 的下载部分下载。它是包含多个配置文件的更大 ZIP 文件的一部分。他们明确建议使用 ISO Coated v2 300% (ECI) 配置文件。

如果您需要将完整图像从 CMYK 转换为 RGB,则同一命名空间中有专门的类。

If you want good result, you need to apply a color profile. In .NET, you can do it like that (assuming the the original CMYK components are in the range between 0 and 255):

float[] colorValues = new float[4];
colorValues[0] = c / 255f;
colorValues[1] = m / 255f;
colorValues[2] = y / 255f;
colorValues[3] = k / 255f;

System.Windows.Media.Color color = Color.FromValues(colorValues,
    new Uri(@"C:\Users\me\Documents\ISOcoated_v2_300_eci.icc"));
System.Drawing.Color rgbColor = System.Drawing.Color.FromArgb(color.R, color.G, color.B);

Note that two different Color classes from two different namespaces are used. And you probably need to add the PresentationCore DLL as a reference.

The required color profile can be downloaded from the downloads section of eci.org. It's part of a bigger ZIP file containing several profiles. They explicitly recommend to use the ISO Coated v2 300% (ECI) profile.

If you need to convert a complete image from CMYK to RGB, there are special classes for this in the same namespace.

三五鸿雁 2024-09-01 12:17:53

我的 C# CMYK <-> 之间转换的完整示例十六进制:

public class ColorConverter
{
    public static string CMYKtoHex(decimal[] cmyk)
    {
        if (cmyk.Length != 4) return null;

        var r = (int)(255 * (1 - cmyk[0]) * (1 - cmyk[3]));
        var g = (int)(255 * (1 - cmyk[1]) * (1 - cmyk[3]));
        var b = (int)(255 * (1 - cmyk[2]) * (1 - cmyk[3]));

        var hex = "#" + r.ToString("X2") + g.ToString("X2") + b.ToString("X2");
        return hex;
    }

    public static decimal[] HexToCMYK(string hex)
    {
        decimal computedC = 0;
        decimal computedM = 0;
        decimal computedY = 0;
        decimal computedK = 0;

        hex = (hex[0] == '#') ? hex.Substring(1, 6) : hex;

        if (hex.Length != 6)
        {
            return null;
        }

        decimal r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
        decimal g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
        decimal b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);

        // BLACK
        if (r == 0 && g == 0 && b == 0)
        {
            computedK = 1;
            return new[] { 0, 0, 0, computedK };
        }

        computedC = 1 - (r / 255);
        computedM = 1 - (g / 255);
        computedY = 1 - (b / 255);

        var minCMY = Math.Min(computedC, Math.Min(computedM, computedY));

        computedC = (computedC - minCMY) / (1 - minCMY);
        computedM = (computedM - minCMY) / (1 - minCMY);
        computedY = (computedY - minCMY) / (1 - minCMY);
        computedK = minCMY;

        return new[] { computedC, computedM, computedY, computedK };
    }
 }

My complete example for C# conversion between CMYK <-> HEX:

public class ColorConverter
{
    public static string CMYKtoHex(decimal[] cmyk)
    {
        if (cmyk.Length != 4) return null;

        var r = (int)(255 * (1 - cmyk[0]) * (1 - cmyk[3]));
        var g = (int)(255 * (1 - cmyk[1]) * (1 - cmyk[3]));
        var b = (int)(255 * (1 - cmyk[2]) * (1 - cmyk[3]));

        var hex = "#" + r.ToString("X2") + g.ToString("X2") + b.ToString("X2");
        return hex;
    }

    public static decimal[] HexToCMYK(string hex)
    {
        decimal computedC = 0;
        decimal computedM = 0;
        decimal computedY = 0;
        decimal computedK = 0;

        hex = (hex[0] == '#') ? hex.Substring(1, 6) : hex;

        if (hex.Length != 6)
        {
            return null;
        }

        decimal r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
        decimal g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
        decimal b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);

        // BLACK
        if (r == 0 && g == 0 && b == 0)
        {
            computedK = 1;
            return new[] { 0, 0, 0, computedK };
        }

        computedC = 1 - (r / 255);
        computedM = 1 - (g / 255);
        computedY = 1 - (b / 255);

        var minCMY = Math.Min(computedC, Math.Min(computedM, computedY));

        computedC = (computedC - minCMY) / (1 - minCMY);
        computedM = (computedM - minCMY) / (1 - minCMY);
        computedY = (computedY - minCMY) / (1 - minCMY);
        computedK = minCMY;

        return new[] { computedC, computedM, computedY, computedK };
    }
 }
白色秋天 2024-09-01 12:17:53

我同意前面的答案,但我想说的是:

if ( K == 1 ) 
{ 
   C = 0
   M = 0
   Y = 0
} 

如果 r = g = b = 0 就可以。

I agree with the previous answers, but I want to say that:

if ( K == 1 ) 
{ 
   C = 0
   M = 0
   Y = 0
} 

It can be if r = g = b = 0.

意中人 2024-09-01 12:17:53

为了将 RGB 转换为 CMYK,我编写了一个名为 ColorHelper 的库。

using ColorHelper;
RGB rgb = new RGB(220, 234, 174);
CMYK cmyk = ColorConverter.RgbToCmyk(rgb);

该库中的转换算法:

public static CMYK RgbToCmyk(RGB rgb)
{
    double modifiedR, modifiedG, modifiedB, c, m, y, k;

    modifiedR = rgb.R / 255.0;
    modifiedG = rgb.G / 255.0;
    modifiedB = rgb.B / 255.0;

    k = 1 - new List<double>() { modifiedR, modifiedG, modifiedB }.Max();
    c = (1 - modifiedR - k) / (1 - k);
    m = (1 - modifiedG - k) / (1 - k);
    y = (1 - modifiedB - k) / (1 - k);

    return new CMYK(
        (byte)Math.Round(c * 100),
        (byte)Math.Round(m * 100),
        (byte)Math.Round(y * 100),
        (byte)Math.Round(k * 100));
}

链接 - https://github .com/iamartyom/ColorHelper/blob/master/ColorHelper/Converter/ColorConverter.cs

For convert RGB to CMYK, I've written a library called ColorHelper.

using ColorHelper;
RGB rgb = new RGB(220, 234, 174);
CMYK cmyk = ColorConverter.RgbToCmyk(rgb);

The conversion algorithm in this library:

public static CMYK RgbToCmyk(RGB rgb)
{
    double modifiedR, modifiedG, modifiedB, c, m, y, k;

    modifiedR = rgb.R / 255.0;
    modifiedG = rgb.G / 255.0;
    modifiedB = rgb.B / 255.0;

    k = 1 - new List<double>() { modifiedR, modifiedG, modifiedB }.Max();
    c = (1 - modifiedR - k) / (1 - k);
    m = (1 - modifiedG - k) / (1 - k);
    y = (1 - modifiedB - k) / (1 - k);

    return new CMYK(
        (byte)Math.Round(c * 100),
        (byte)Math.Round(m * 100),
        (byte)Math.Round(y * 100),
        (byte)Math.Round(k * 100));
}

Link - https://github.com/iamartyom/ColorHelper/blob/master/ColorHelper/Converter/ColorConverter.cs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文