简体   繁体   English

将RGB颜色转换为CMYK?

[英]Convert RGB color to CMYK?

I'm looking for an algorithm to convert an RGB color to CMYK.我正在寻找一种将 RGB 颜色转换为 CMYK 的算法。 Photoshop is performing the conversion below: Photoshop 正在执行以下转换:

R = 220 G = 233 B = 174 R = 220 G = 233 B = 174

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

The conversion from RGB to CMYK is dependent on the physical device/process being used to lay down the CMYK ink.从 RGB 到 CMYK 的转换取决于用于放置 CMYK 墨水的物理设备/过程。 These are represented in software as Color Profiles.这些在软件中表示为颜色配置文件。 ICC and ICM color profiles of physical devices determine the resulting colors.物理设备的ICCICM颜色配置文件决定了生成的颜色。

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).但是,如果您关心这方面,那么您需要使用Windows 颜色管理 APILittleCMS 之类的东西来为您进行颜色转换(因为它们在转换过程中应用了正确的颜色配置文件)。

Check out this link: http://www.codeproject.com/KB/applications/xcmyk.aspx .查看此链接: 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) 

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):在 .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);

Note that two different Color classes from two different namespaces are used.请注意,使用了来自两个不同命名空间的两个不同 Color 类。 And you probably need to add the PresentationCore DLL as a reference.并且您可能需要添加 PresentationCore DLL 作为参考。

The required color profile can be downloaded from the downloads section of eci.org .可以从eci.org的下载部分下载所需的颜色配置文件。 It's part of a bigger ZIP file containing several profiles.它是包含多个配置文件的更大 ZIP 文件的一部分。 They explicitly recommend to use the ISO Coated v2 300% (ECI) profile.他们明确建议使用 ISO Coated v2 300% (ECI) 配置文件。

If you need to convert a complete image from CMYK to RGB, there are special classes for this in the same namespace.如果您需要将一个完整的图像从 CMYK 转换为 RGB,在同一个命名空间中有专门的类。

My complete example for C# conversion between CMYK <-> HEX:我在 CMYK <-> HEX 之间进行 C# 转换的完整示例:

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

I think photoshop uses profile based conversions.我认为 Photoshop 使用基于配置文件的转换。 Have a look at the documentation for the color options .查看颜色选项的文档 If it is the case you won't be able to match this behavior with simple formula based solutions.如果是这种情况,您将无法将此行为与基于简单公式的解决方案相匹配。

I have found LCMS to be very handy, simple and efficient for this kind of task.I don't known if there is any .net binding for it...我发现 LCMS 对于此类任务非常方便、简单且高效。我不知道是否有任何 .net 绑定...

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.可以是如果 r = g = b = 0。

For convert RGB to CMYK you can use ColorHelper library.要将 RGB 转换为 CMYK,您可以使用ColorHelper库。

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

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链接 - https://github.com/iamartyom/ColorHelper/blob/master/ColorHelper/Converter/ColorConverter.cs

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

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