CMYK颜色标准是彩色印刷机标准,在我们开发程序中对于颜色只有RGB模式的在C#语言中就是Color类来代表的。
它们之间颜色效果会存在一些差异,为了减少这些差异可以通过一下计算方式去匹配出对应的颜色数据。
////// CMYK列印颜色 /// public sealed class CMYKColor { #region --- 成员 Begin --- [CompilerGenerated] private double _C; ////// C值 /// public double C { [CompilerGenerated] get { return _C; } [CompilerGenerated] set { _C = value; } } [CompilerGenerated] private double _M; ////// M值 /// public double M { [CompilerGenerated] get { return _M; } [CompilerGenerated] set { _M = value; } } [CompilerGenerated] private double _Y; ////// Y值 /// public double Y { [CompilerGenerated] get { return _Y; } [CompilerGenerated] set { _Y = value; } } [CompilerGenerated] private double _K; ////// K值 /// public double K { [CompilerGenerated] get { return _K; } [CompilerGenerated] set { _K = value; } } #endregion --- 成员 End --- #region --- 构造方法 Begin --- ////// CMYKColor默认构造方法 /// public CMYKColor() { this._C = 0.0; this._M = 0.0; this._Y = 0.0; this._K = 1.0; } ////// CMYKColor使用Int值模式赋值 /// /// C 取值范围0~100 /// M 取值范围0~100 /// Y 取值范围0~100 /// K 取值范围0~100 public CMYKColor(int c, int m, int y, int k) { if (c >= 0 && c <= 100) { this._C = c / 100.0; } else { this._C = 0.0; } if (m >= 0 && m <= 100) { this._M = m / 100.0; } else { this._M = 0.0; } if (y >= 0 && y <= 100) { this._Y = y / 100.0; } else { this._Y = 0.0; } if (k >= 0 && k <= 100) { this._K = k / 100.0; } else { this._K = 1.0; } } ////// CMYKColor使用Double值模式赋值 /// /// C 取值范围1~0.00 /// M 取值范围1~0.00 /// Y 取值范围1~0.00 /// K 取值范围1~0.00 public CMYKColor(double c, double m, double y, double k) { this._C = c; this._M = m; this._Y = y; this._K = k; } ////// CMYKColor使用System.Drawing.Color赋值 /// /// 颜色类型 public CMYKColor(Color color) { this.ColorToCMYKColor(color); } #endregion --- 构造方法 End --- #region --- 转换方法 Begin --- ////// 从颜色类型转换为CMYKColor /// /// 需要转换的颜色 public void ColorToCMYKColor(Color color) { double numR = ((double)(0xff - color.R)) / 255; double numG = ((double)(0xff - color.G)) / 255; double numB = ((double)(0xff - color.B)) / 255; double numContrast = Math.Min(numR, Math.Min(numG, numB)); if (numContrast == 1) { this._C = 0.0; this._M = 0.0; this._Y = 0.0; this._K = 1.0; } else { this._C = (numR - numContrast) / (1 - numContrast); this._M = (numG - numContrast) / (1 - numContrast); this._Y = (numB - numContrast) / (1 - numContrast); this._K = numContrast; } } ////// 从CMYK转换为Color /// ///public Color CMYKColorToColor() { return Color.FromArgb(Convert.ToInt32((double)(((1 - this._C) * (1 - this._K)) * 255)), Convert.ToInt32((double)(((1 - this._M) * (1 - this._K)) * 255)), Convert.ToInt32((double)(((1 - this._Y) * (1 - this._K)) * 255))); } /// /// 获取CMYK的百分比 /// /// C or M or Y or K /// 小数后多少位 ///public double GetPercent(double value, int digits) { return Math.Round((double)(value * 100), digits); } #endregion --- 转换方法 End --- /// /// 显示CMYK各值的百分比 /// ///public override string ToString() { int digits = 2; return string.Format("C={0}%; M={1}%; Y={2}%; K={3}%", this.GetPercent(this._C, digits), this.GetPercent(this._M, digits), this.GetPercent(this._Y, digits), this.GetPercent(this._K, digits)); } }