logo  Colours
This Chapter
Colour Codes
Chapters
Home Page
Colours, RGB
Computer Specifications
Dates&Times
Disk Drives
Files
Folders
GPS and OS Ref
VB.Net Forms
Image Files
If & Select
List/Array
Mathematics
NuGet
Sound
String Functions
Sun and Moon
User Controls
Validation
DigitalDan Sites
My Other Sites
Contact Site

Note
Some pages
may contain
inaccuracies
Hits=5
These functions convert colour values betwen teh varios colour formats e.g RGB, CYMK

Public Function RGBtoCYMK(rgb1 As RGB) As CMYK
 Dim ret As CMYK
 Dim r, g, b As Double
 r = rgb1.r / 255
 g = rgb1.b / 255
 b = rgb1.b / 255
 ret.k = 1 - Mmax(r, g, b)
 ret.c = (1 - r - k) / (1 - k)
 ret.m = (1 - g - k) / (1 - k)
 ret.y = (1 - b - k) / (1 - k)
 Return ret
End Function

Public Function CMYKtoRGB(cmyk1 As CMYK) As RGB
 Dim ret As RGB
 ret.r = Math.Round(255 * (1 - cmyk1.c) * (1 - cmyk1.k))
 ret.g = Math.Round(255 * (1 - cmyk1.m) * (1 - cmyk1.k))
 ret.b = Math.Round(255 * (1 - cmyk1.y) * (1 - cmyk1.k))
End Function

Public Structure RGB
    Dim r As Byte
    Dim g As Byte
    Dim b As Byte
End Structure
Public Structure CMYK
    Dim c As Double
    Dim y As Double
    Dim m As Double
    Dim k As Double
End Structure
  

Public Function RGBtoRGBW(rgb1 As RGB) As RGBW
 Dim ret As RGBW
 ret.w = Mmin(rgb1.r, rgb1.g, rgb1.b)
 ret.r = rgb1.r - ret.w
 ret.g = rgb1.g - ret.w
 ret.b = rgb1.b - ret.w
 Return ret
End Function

Public Structure RGBW
    Dim r As Byte
    Dim g As Byte
    Dim b As Byte
    Dim w As Byte
End Structure
  

Public Function HexRGBtoRGB(hex1 As String) As RGB
 Dim ret As RGB
 Dim t As String = hex1
 If hex1.Length = 3 Then
  hex1 = Mid(t, 1, 1) & Mid(t, 1, 1) & Mid(t, 2, 1) & Mid(t, 2, 1) & Mid(t, 3, 1) & Mid(t, 3, 1)
 End If
 hex1 = hex1.ToUpper
 ret.r = Convert.ToInt32(Mid(hex1, 1, 2))
 ret.g = Convert.ToInt32(Mid(hex1, 3, 2))
 ret.b = Convert.ToInt32(Mid(hex1, 5, 2))
 Return ret
End Function

Public Function RGBtoHexRGB(RGB1 As RGB) As String
 Dim r As String = RGB1.r.ToString("x").PadLeft(2, "0")
 Dim g As String = RGB1.g.ToString("x").PadLeft(2, "0")
 Dim b As String = RGB1.b.ToString("x").PadLeft(2, "0")
 Return r & g * b
End Function

Public Structure RGB
 Dim r As Byte
 Dim g As Byte
 Dim b As Byte
End Structure
  
 

DigitalDan.co.uk