Color Conversions (RGB, CMYK, HSB)
Author Message Color Conversions (RGB, CMYK, HSB)

In one of my projects, I wanted to allow the user to select colors using
CMYK, RGB, or HSB coding schemes. I've found the code to convert CMYK
colors to RGB, but I can't seem to find ANYTHING about HSB (aka HLS or
HSV). Obviously, there is *some* way to convert HSB to RGB, because the VB
color palette lets you specify colors in hue, saturation and brightness.

So here are my questions:

1. Is there a way to convert RGB colors to CMYK? If so, does anyone know
the algorithm?

2. Is there a way to convert HSB values to RGB? If so, does anyone know the
algorithm?

P.S. For those interested, here's the code to convert CMYK to RGB:

Option Explicit

Function GetRGBFromCMYK(ByVal c As Byte, _
ByVal m As Byte, _
ByVal y As Byte, _
ByVal k As Byte) _
As Long

'// Returns the RGB equivalent of the specified CMYK color.
'// c, m, y, k must be from 1 to 100

Assert c >= 0 And c <= 100
Assert m >= 0 And m <= 100
Assert y >= 0 And y <= 100

Dim chRed As Byte
Dim chGreen As Byte
Dim chBlue As Byte
Dim rgbValue As Long

chRed = ExtractColor(c, k)
chGreen = ExtractColor(m, k)
chBlue = ExtractColor(y, k)

rgbValue = rgb(chRed, chGreen, chBlue)

GetRGBFromCMYK = rgbValue

End Function

Function Max(v1 As Variant, v2 As Variant) As Variant

'// Returns the greater of two values.

Min = IIf(v1 > v2, v1, v2)

End Function

Function Min(v1 As Variant, v2 As Variant) As Variant

'// Returns the lesser of two values.

Min = IIf(v1 < v2, v1, v2)

End Function

Function ExtractColor(chColor As Byte, chBlack As Byte) As Byte

'// Extracts an RGB color component from a CMYK color.

Dim ch As Byte

ch = (100 - chColor) - chBlack
ch = Min(ch, 255)
ch = ch * 2.56
ch = Max(ch, 0)

ExtractColor = ch

End Function

--
Michael A. Hofer

Sat, 23 Jan 1999 03:00:00 GMT

 Page 1 of 1 [ 1 post ]

Relevant Pages