Convert RGB color to CMYK?

前端 未结 7 1707
谎友^
谎友^ 2020-11-28 12:09

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 =

相关标签:
7条回答
  • 2020-11-28 13:07

    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 };
        }
     }
    
    0 讨论(0)
提交回复
热议问题