Last active
June 20, 2019 01:59
-
-
Save 0V/26289851d68955ef8aa5 to your computer and use it in GitHub Desktop.
RGB ⇔ CMYK by C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ColorConverter | |
{ | |
class CmykColorConverter | |
{ | |
static void Main(string[] args) | |
{ | |
// OpenCV の色表現は BGR なので順番注意 | |
// 論理式としての意味合いが強いため、色の再現性は不明 | |
var cmyk = RgbToCmyk(12, 21, 213);// .Select(x => { Console.WriteLine(x); return x; }).ToList(); | |
var rgb = CmykToRgb(0.78, 0.21, 0.34, 0.24);// .Select(x => { Console.WriteLine(x); return x; }).ToList(); | |
} | |
static double[] RgbToCmyk(byte red, byte green, byte blue) | |
{ | |
double black = Math.Min(1.0 - red / 255.0, Math.Min(1.0 - green / 255.0, 1.0 - blue / 255.0)); | |
double cyan = (1.0 - (red / 255.0) - black) / (1.0 - black); | |
double magenta = (1.0 - (green / 255.0) - black) / (1.0 - black); | |
double yellow = (1.0 - (blue / 255.0) - black) / (1.0 - black); | |
return new[] { cyan, magenta, yellow, black }; | |
} | |
static byte[] CmykToRgb(double cyan, double magenta, double yellow, double black) | |
{ | |
byte red = Convert.ToByte((1 - Math.Min(1, cyan * (1 - black) + black)) * 255); | |
byte green = Convert.ToByte((1 - Math.Min(1, magenta * (1 - black) + black)) * 255); | |
byte blue = Convert.ToByte((1 - Math.Min(1, yellow * (1 - black) + black)) * 255); | |
return new[] { red, green, blue }; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment