Archive Browser
Download CmykRgbColorConversion.zip, last updated 15/07/2018 (19.21 KB)
Download- md5: 1258961adc735c8388f9b35d867df7c6
- sha1: 1e01eb8723aac6632f1c0fff5fb57f524116329c
- sha256: cfc2141a0f0820962621bc35e10882e648c95496751b65ded610a0009faae5a6
using System;
using System.Drawing;
// Converting colours between RGB and CMYK in C#
// https://www.cyotek.com/blog/converting-colours-between-rgb-and-cmyk-in-csharp
// Copyright © 2018 Cyotek Ltd. All Rights Reserved.
// This work is licensed under the Creative Commons Attribution 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/.
// Found this example useful?
// https://www.paypal.me/cyotek
namespace Cyotek.Demo.CmykRgbColorConversion
{
public static class ColorConvert
{
#region Static Methods
public static Color ConvertCmykToRgb(float c, float m, float y, float k)
{
int r;
int g;
int b;
// Conversion forumla taken from http://www.rapidtables.com/convert/color/cmyk-to-rgb.htm
// assumes cmyk values are in the range 0-1
r = Convert.ToInt32(255 * (1 - c) * (1 - k));
g = Convert.ToInt32(255 * (1 - m) * (1 - k));
b = Convert.ToInt32(255 * (1 - y) * (1 - k));
return Color.FromArgb(r, g, b);
}
public static CmykColor ConvertRgbToCmyk(int r, int g, int b)
{
float c;
float m;
float y;
float k;
float rf;
float gf;
float bf;
rf = r / 255F;
gf = g / 255F;
bf = b / 255F;
k = ClampCmyk(1 - Math.Max(Math.Max(rf, gf), bf));
c = ClampCmyk((1 - rf - k) / (1 - k));
m = ClampCmyk((1 - gf - k) / (1 - k));
y = ClampCmyk((1 - bf - k) / (1 - k));
return new CmykColor(c, m, y, k);
}
private static float ClampCmyk(float value)
{
if (value < 0 || float.IsNaN(value))
{
value = 0;
}
return value;
}
#endregion
}
}
Donate
This software may be used free of charge, but as with all free software there are costs involved to develop and maintain.
If this site or its services have saved you time, please consider a donation to help with running costs and timely updates.
Donate