0% found this document useful (0 votes)
19 views7 pages

RGB Colours

The document provides a comprehensive guide to the RGB color model, detailing the additive mixing of red, green, and blue to create various colors, including a list of eight primary colors and their RGB values. It also covers the VBA ColorIndex property, explaining its syntax, examples, and the 56 colors available, alongside methods for converting RGB values to decimal and vice versa. Additionally, it includes code snippets for generating color representations in Excel using VBA.

Uploaded by

G RAVI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

RGB Colours

The document provides a comprehensive guide to the RGB color model, detailing the additive mixing of red, green, and blue to create various colors, including a list of eight primary colors and their RGB values. It also covers the VBA ColorIndex property, explaining its syntax, examples, and the 56 colors available, alongside methods for converting RGB values to decimal and vice versa. Additionally, it includes code snippets for generating color representations in Excel using VBA.

Uploaded by

G RAVI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

2/1/25, 12:21 PM RGB colours

VBA colors

0. Quick guide to the RGB color model

In this module:

1. RGB color circles

2. ColorIndex property - 56 colours, and VBA ColorConstants


3. RGB and decimal color values, plus conversion

4. Color property

The RGB color model adds combinations of Red, Green, and Blue to produce various colours. Each component is an integer value 0 to 255. The RGB colors with
intersection overlap is demonstrated in figure 1.

1. RGB colors

1.1 RGB circles with additive colour mixing

Three RGB circles with colour mixing - returns 7 colors.

Fig 1: - three circles; Red, Green, and Blue (RGB), with 4 intersection points

With the inclusion of black (no colour), the eight colours are:

1. Black: RGB(0,0,0)
2. White: RGB(255,255,255)

3. Red: RGB(255,0,0)
4. Green: RGB(0,255,0)

5. Blue: RGB(0,0,255)

6. Yellow: RGB(255,255,0)

7. Magenta: RGB(255,0,255)

8. Cyan: RGB(0,255,255)

1.2 RGB circles - production code

Using web browser CSS and Scalable Vector Graphics (SVG)

Code for CSS style and Scalable Vector Graphics (SVG) used to produce the RGB circles in figure 1

https://excelatfinance.com/xlf/xlf-colors-1.php 1/7
2/1/25, 12:21 PM RGB colours
1 <style type="text/css">
2 circle {mix-blend-mode: screen;}
3 </style>
4
5 <svg>
6 <circle cx="75" cy="50" r="40" fill="rgb(255,0,0)"></circle>
7 <circle cx="50" cy="100" r="40" fill="rgb(0,255,0)"></circle>
8 <circle cx="100" cy="100" r="40" fill="rgb(0,0,255)"></circle>
9 </svg>
10

2. VBA ColorIndex property

Syntax: expression.ColorIndex = value

where value is an element from the integer series 1,2, …, 56. Special values include xlColorIndexAutomatic (-4105) and xlColorIndexNone (-4142).

Examples of the ColorIndex property:

xlRange.Value = "excel"

xlRange.Interior.ColorIndex = 48

xlRange.Font.ColorIndex = 20

xlRange.Borders.ColorIndex = 3
xlRange.Characters(1, 2).Font.ColorIndex = 6

2.1 The 56 colours of ColorIndex

Colours 2 to 8 are red, green, and blue with additive mixing. The colours 9 to 56 are various combinations of red, green and blue with RGB values: 0, 51, 102, 128,
150, 153, 192, 204, and 255 (figure 2).

https://excelatfinance.com/xlf/xlf-colors-1.php 2/7
2/1/25, 12:21 PM RGB colours

Fig 2: ColorIndex - numbers, colours, and RGB values from code 1

The WS range in figure 2 was printed from the ColIndx2wWS procedure in code 1.

Code 1: Sub ColIndx2WS procedure prints ColorIndex to WS (see figure 2)

15 Sub ColIndx2WS()
16 ' Requires xlfDec2RGB(ColDec) function
17 Dim i As Integer
18 Dim AC As Range: Set AC = ActiveCell
19
20 For i = 1 To 56
21 AC(i, 1) = i
22 AC(i, 1).HorizontalAlignment = xlLeft
23 AC(i, 2).Interior.ColorIndex = i
24 AC(i, 3).Value = "RGB(" & xlfDec2RGB(AC(i, 2).Interior.Color) & ")" ' see code ...
25 AC(i, 3).HorizontalAlignment = xlRight
26 Next i
27 AC(i, 3).ColumnWidth = 19
28 End Sub

The colour table can also be printed as a 7 by 8 array (code 2)

https://excelatfinance.com/xlf/xlf-colors-1.php 3/7
2/1/25, 12:21 PM RGB colours

Fig 3: - VBA ColorIndex 56 colors, 7 x 8 array

Code 2: Sub ColIndx2WSarraS procedure prints ColorIndex to WS array (7 rows by 8 columns)

30 Sub ColIndx2WSarray()
31 Const TLC As String = "B2" ' top left cell
32 Dim i As Integer, j As Integer
33 Dim Col As Integer, Val As Integer
34
35 With Range(TLC)
36 For i = 1 To 7
37 For j = 1 To 8
38 Col = i * j
39 Val = Val + 1
40 .Offset(i - 1, j - 1).Interior.ColorIndex = Col
41 .Offset(i - 1, j - 1).Value = Val
42 Select Case Col
43 Case 1, 5, 9 To 14, 16, 18, 21, 23, 25, 29, 30, 32, 41, 47 To 49, 51 To 56
44 .Offset(i - 1, j - 1).Font.ColorIndex = 2
45 Case Else
46 .Offset(i - 1, j - 1).Font.ColorIndex = 1
47 End Select
48 Next j
49 Next i
50 .Resize(7, 8).ColumnWidth = 5
51 .Resize(7, 8).RowHeight = 20
52 End With
53
54 End Sub

Ten of the ColorIndex colours are duplicate pairs: Blue 5 and 32 ; Yellow 6 and 27 ; Pink 7 and 26 ; Turquoise 8 and 28 ; Dark
Red 9 and 30 ; Dark Blue 11 and 25 ; Violet 13 and 29 ; Teal 14 and 31 ; [No name] 18 and 54 ; and [No name] 20 and
34 . Leaving only 46 unique colours.

2.2 ColorConstants

The 8 colours listed in section 1.1 have name equivalents listed as members of the VBA ColorConstants class in the decimal colour system. The ColorConstants
Auto List drop down is shown in figure 4.

Fig 4: VBA ColorConstants - VBA Auto List drop down with 8 items

https://excelatfinance.com/xlf/xlf-colors-1.php 4/7
2/1/25, 12:21 PM RGB colours

Code 3 prints a list of the ColorConstants numerical values to the immediate window (figure 5).

Code 3: Sub ColConst procedure

60 Sub ColConst()
61 Dim i As Integer
62 Dim ColArrVal As Variant
63 Dim ColArrLbl As Variant
64
65 ColArrVal = Array(vbBlack, vbWhite, vbRed, vbGreen, vbBlue, vbYellow, vbMagenta, vbCyan)
66 ColArrLbl = Array("vbBlack", "vbWhite", "vbRed", "vbGreen", "vbBlue", "vbYellow", "vbMagenta", "vbCyan")
67
68 For i = LBound(ColArrVal) To UBound(ColArrVal)
69 Debug.Print ColArrLbl(i) & ": " & ColArrVal(i)
70 Next i
71
72 End Sub

The immediate window with output from line 69

Fig 5: VBA ColorConstants - colors 1 to 8

3. Decimal values of colours

3.1 RGB to positive decimal

To convert RGB to decimal , the relationship is Cd


0 1 2
= R ∗ 256 + (G ∗ 256 ) + (B ∗ 256 ) . See code 4.

Code 4: Function xlfRGB2DecX converts RGB values to decimal. Includes test routine

75 Function xlfRGB2DecX(Red As Integer, Green As Integer, Blue As Integer) As Long


76 xlfRGB2DecX = Red * 256 ^ 0 + Green * 256 ^ 1 + Blue * 256 ^ 2
77 End Function
78
79 ' ===========================
80 Function xlfRGB2DecY(Red As Integer, Green As Integer, Blue As Integer) As Long
81 xlfRGB2DecY = RGB(Red, Green, Blue)
82 End Function
83
84 ' ===========================
85 Sub TestxlfRGB2DecX()
86 Dim Ans As Long
87 Ans = xlfRGB2DecX(204, 255, 255)
88 End Sub

3.2 RGB to negative decimal

To convert RGB to Excel negative decimal, the relationship is C−d 3


= RGB − (256 ) − 1

The color red is RGB(255,0,0), equal to 256 as a positive value. The Excel negative value equivalent is RGB - (256^3) - 1 = 256 - (256^3) - 1 = -16776961

3.3 Decimal to RGB

https://excelatfinance.com/xlf/xlf-colors-1.php 5/7
2/1/25, 12:21 PM RGB colours
The backslash operator "\" divides two numbers and returns the integer quotient. The remainder is ignored.

Code 5: Function xlfDec2RGB converts color decimal to RGB comma separated values. Includes test routine

90 Function xlfDec2RGB(ByVal ColDec As Long) As String


91 Dim R As Long
92 Dim G As Long
93 Dim b As Long
94
95 R = (ColDec And &HFF) \ 256 ^ 0 ' &HFF hexadecimal = 255 decimal
96 ' leading &H is the prefix radix (base) for hexadecimal
97 G = (ColDec And &HFF00&) \ 256 ^ 1 ' &HFF00& hexadecimal = 65280 decimal
98 ' trailing & is a TDC for type long, if
99 ' omitted (&HFF00), the assigned value is -256
100 b = ColDec \ 256 ^ 2
101 xlfDec2RGB = CStr(R) & "," & CStr(G) & "," & CStr(b)
102
103 End Function
104
105 ' ===========================
106 Sub TestxlfDec2RGB()
107 Dim Ans As String
108 Ans = xlfDec2RGB(16737843) ' returns 51,102,255
109 Stop
110 End Sub

About code 5

Line 95: 16737843 And 255 returns 51, 51 \ 1 returns 51, remainder 0

Further details of the binary And are provided in figure 5. Only the last 8 digits on the right are relevant.
16737843 = 111111110110011000110011;

255 = 11111111;
AND returns 00110011 = 51

Fig 5: binary AND - last 8 digits on the right

Line 97: 16737843 And 65280 returns 26112, 26112 \ 256 returns 102, remainder 0
Line 100: 16737843 \ 65536 returns 255, remainder 2616

4. Color property

Syntax: expression.Color = value

where value can be created by the RGB function returned as a long whole number.

Examples of the Color property:

xlRange.Value = "excel"
xlRange.Interior.Color = RGB(150,150,150) (equivalent to ColorIndex 48)

xlRange.Font.Color = 16777164 (equivalent to: RGB(204,255,255); ColorIndex 48)


xlRange.Borders.Color = RGB(150,0,0) (equivalent to ColorIndex 3)

xlRange.Characters(1, 2).Font.Color = 6 (equivalent to: RGB(6,0,0))

RGB discussion [23 Apr 2018]

https://excelatfinance.com/xlf/xlf-colors-1.php 6/7
2/1/25, 12:21 PM RGB colours
Development platform: Excel 2016 64 bit.
Published: 14th April 2016
Revised: Friday 24th of February 2023 - 02:37 PM, Pacific Time (PT)

Copyright © 2011 – 2025 ♦ Ian O'Connor, Central Gippsland | Privacy policy

Comment & Reply policy

https://excelatfinance.com/xlf/xlf-colors-1.php 7/7

You might also like