RGB To CMYK

This algorithm converts RGB color model to CMYK color model.



									Public Structure CMYK
	Private _c As Double
	Private _m As Double
	Private _y As Double
	Private _k As Double

	Public Sub New(c As Double, m As Double, y As Double, k As Double)
		Me._c = c
		Me._m = m
		Me._y = y
		Me._k = k
	End Sub

	Public Property C() As Double
		Get
			Return Me._c
		End Get
		Set(value As Double)
			Me._c = value
		End Set
	End Property

	Public Property M() As Double
		Get
			Return Me._m
		End Get
		Set(value As Double)
			Me._m = value
		End Set
	End Property

	Public Property Y() As Double
		Get
			Return Me._y
		End Get
		Set(value As Double)
			Me._y = value
		End Set
	End Property

	Public Property K() As Double
		Get
			Return Me._k
		End Get
		Set(value As Double)
			Me._k = value
		End Set
	End Property

	Public Overloads Function Equals(cmyk As CMYK) As Boolean
		Return (Me.C = cmyk.C) AndAlso (Me.M = cmyk.M) AndAlso (Me.Y = cmyk.Y) AndAlso (Me.K = cmyk.K)
	End Function
End Structure

Public Structure RGB
	Private _r As Byte
	Private _g As Byte
	Private _b As Byte

	Public Sub New(r As Byte, g As Byte, b As Byte)
		Me._r = r
		Me._g = g
		Me._b = b
	End Sub

	Public Property R() As Byte
		Get
			Return Me._r
		End Get
		Set(value As Byte)
			Me._r = value
		End Set
	End Property

	Public Property G() As Byte
		Get
			Return Me._g
		End Get
		Set(value As Byte)
			Me._g = value
		End Set
	End Property

	Public Property B() As Byte
		Get
			Return Me._b
		End Get
		Set(value As Byte)
			Me._b = value
		End Set
	End Property

	Public Overloads Function Equals(rgb As RGB) As Boolean
		Return (Me.R = rgb.R) AndAlso (Me.G = rgb.G) AndAlso (Me.B = rgb.B)
	End Function
End Structure

Public Shared Function RGBToCMYK(rgb As RGB) As CMYK
	Dim dr As Double = CDbl(rgb.R) / 255
	Dim dg As Double = CDbl(rgb.G) / 255
	Dim db As Double = CDbl(rgb.B) / 255
	Dim k As Double = 1 - Math.Max(Math.Max(dr, dg), db)
	Dim c As Double = (1 - dr - k) / (1 - k)
	Dim m As Double = (1 - dg - k) / (1 - k)
	Dim y As Double = (1 - db - k) / (1 - k)

	Return New CMYK(c, m, y, k)
End Function
								


Example

									Dim data As New RGB(125, 29, 107)
Dim value = RGBToCMYK(data)
								


Output

									C: 0
M: 0.7679999999999999
Y: 0.14399999999999993
K: 0.50980392156862742