vb6parse 1.0.0

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="VB6Parse Library Reference - rgb - Graphics">
    <title>rgb - Graphics - VB6Parse Library Reference</title>
    <link rel="stylesheet" href="../../../assets/css/style.css">
    <link rel="stylesheet" href="../../../assets/css/docs-style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
    <script src="../../../assets/js/theme-switcher.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
    <script>hljs.highlightAll();</script>
</head>
<body>
    <header class="docs-header">
        <div class="container">
            <h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/graphics/index.html">Graphics</a> / rgb</h1>
            <p class="tagline">VB6 Library Reference</p>
        </div>
    </header>

    <nav class="docs-nav">
        <div class="container">
            <a href="../../../index.html">Home</a>
            <a href="../../../library/index.html">Library Reference</a>
            <a href="../../../documentation.html">Documentation</a>
            <a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
            <a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
            <button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
                <span class="theme-icon">🌙</span>
            </button>
        </div>
    </nav>

    <main class="container">
        
        <article class="library-item">
            <h1 id="rgb-function">RGB Function</h1>
<p>Returns a Long representing an RGB color value from red, green, and blue color components.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">RGB(red, green, blue)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>red</code> - Required. Integer in the range 0-255 that represents the red component of the color.</li>
<li><code>green</code> - Required. Integer in the range 0-255 that represents the green component of the color.</li>
<li><code>blue</code> - Required. Integer in the range 0-255 that represents the blue component of the color.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>Long</code> representing the RGB color value. The value is calculated as:</p>
<pre><code class="language-text">RGB = red + (green * 256) + (blue * 65536)</code></pre>
<p>The return value is a Long integer in BGR (Blue-Green-Red) byte order, which is the standard format for Windows color values.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>RGB</code> function creates custom color values by combining red, green, and blue components. This is the primary way to specify colors programmatically in VB6 beyond the predefined color constants.
Each color component must be in the range 0-255:
- 0 represents no intensity (color is off)
- 255 represents maximum intensity (color is fully on)
- Values outside this range are automatically adjusted to fit within 0-255
<strong>Important Notes</strong>:
- The return value uses BGR byte order (not RGB order) for Windows compatibility
- RGB(0, 0, 0) = Black (0x000000)
- RGB(255, 255, 255) = White (0xFFFFFF)
- RGB(255, 0, 0) = Red (0x0000FF in BGR format)
- RGB(0, 255, 0) = Green (0x00FF00)
- RGB(0, 0, 255) = Blue (0xFF0000 in BGR format)
- Values greater than 255 are treated as 255 (saturated)
- Negative values are treated as 0
<strong>Color Mixing</strong>:
- RGB(255, 0, 0) = Pure Red
- RGB(0, 255, 0) = Pure Green
- RGB(0, 0, 255) = Pure Blue
- RGB(255, 255, 0) = Yellow (Red + Green)
- RGB(255, 0, 255) = Magenta (Red + Blue)
- RGB(0, 255, 255) = Cyan (Green + Blue)
- RGB(128, 128, 128) = Gray (equal components)</p>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Custom Colors</strong>: Create specific colors not available as constants</li>
<li><strong>Dynamic Coloring</strong>: Calculate colors based on data values</li>
<li><strong>Gradients</strong>: Create smooth color transitions</li>
<li><strong>Color Schemes</strong>: Define coordinated color palettes</li>
<li><strong>Data Visualization</strong>: Color-code data points, charts, or graphs</li>
<li><strong>User Preferences</strong>: Allow users to select custom colors</li>
<li><strong>Theme Systems</strong>: Implement application-wide color themes</li>
<li><strong>Image Processing</strong>: Manipulate individual pixel colors</li>
</ol>
<h2 id="basic-examples">Basic Examples</h2>
<h3 id="example-1-primary-colors">Example 1: Primary Colors</h3>
<pre><code class="language-vbnet">&#x27; Set form background to pure red
Form1.BackColor = RGB(255, 0, 0)
&#x27; Set label to pure green
Label1.ForeColor = RGB(0, 255, 0)
&#x27; Set button to pure blue
Command1.BackColor = RGB(0, 0, 255)</code></pre>
<h3 id="example-2-custom-colors">Example 2: Custom Colors</h3>
<pre><code class="language-vbnet">&#x27; Create a nice orange color
Dim orange As Long
orange = RGB(255, 165, 0)
&#x27; Create a purple color
Dim purple As Long
purple = RGB(128, 0, 128)
&#x27; Create a brown color
Dim brown As Long
brown = RGB(165, 42, 42)</code></pre>
<h3 id="example-3-shades-of-gray">Example 3: Shades of Gray</h3>
<pre><code class="language-vbnet">&#x27; Create various shades of gray
Dim lightGray As Long
Dim mediumGray As Long
Dim darkGray As Long
lightGray = RGB(211, 211, 211)
mediumGray = RGB(128, 128, 128)
darkGray = RGB(64, 64, 64)</code></pre>
<h3 id="example-4-data-driven-coloring">Example 4: Data-Driven Coloring</h3>
<pre><code class="language-vbnet">&#x27; Color-code values based on magnitude
Dim value As Double
Dim cellColor As Long
If value &gt; 100 Then
    cellColor = RGB(255, 0, 0)      &#x27; Red for high values
ElseIf value &gt; 50 Then
    cellColor = RGB(255, 255, 0)    &#x27; Yellow for medium values
Else
    cellColor = RGB(0, 255, 0)      &#x27; Green for low values
End If</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="pattern-1-creategradient">Pattern 1: <code>CreateGradient</code></h3>
<pre><code class="language-vbnet">Function CreateGradient(startColor As Long, endColor As Long, _
                       steps As Integer, stepNum As Integer) As Long
    &#x27; Create a color between startColor and endColor
    Dim startR As Integer, startG As Integer, startB As Integer
    Dim endR As Integer, endG As Integer, endB As Integer
    Dim r As Integer, g As Integer, b As Integer
    Dim ratio As Double
    &#x27; Extract RGB components from start color
    startR = startColor And &amp;HFF
    startG = (startColor \ 256) And &amp;HFF
    startB = (startColor \ 65536) And &amp;HFF
    &#x27; Extract RGB components from end color
    endR = endColor And &amp;HFF
    endG = (endColor \ 256) And &amp;HFF
    endB = (endColor \ 65536) And &amp;HFF
    &#x27; Calculate interpolation ratio
    ratio = stepNum / steps
    &#x27; Interpolate each component
    r = startR + ((endR - startR) * ratio)
    g = startG + ((endG - startG) * ratio)
    b = startB + ((endB - startB) * ratio)
    CreateGradient = RGB(r, g, b)
End Function</code></pre>
<h3 id="pattern-2-extractcolorcomponents">Pattern 2: <code>ExtractColorComponents</code></h3>
<pre><code class="language-vbnet">Sub ExtractRGB(color As Long, red As Integer, green As Integer, blue As Integer)
    &#x27; Extract individual RGB components from a color value
    red = color And &amp;HFF
    green = (color \ 256) And &amp;HFF
    blue = (color \ 65536) And &amp;HFF
End Sub</code></pre>
<h3 id="pattern-3-lightencolor">Pattern 3: <code>LightenColor</code></h3>
<pre><code class="language-vbnet">Function LightenColor(color As Long, percent As Double) As Long
    &#x27; Lighten a color by a percentage (0-100)
    Dim r As Integer, g As Integer, b As Integer
    r = color And &amp;HFF
    g = (color \ 256) And &amp;HFF
    b = (color \ 65536) And &amp;HFF
    &#x27; Increase each component toward 255
    r = r + ((255 - r) * percent / 100)
    g = g + ((255 - g) * percent / 100)
    b = b + ((255 - b) * percent / 100)
    LightenColor = RGB(r, g, b)
End Function</code></pre>
<h3 id="pattern-4-darkencolor">Pattern 4: <code>DarkenColor</code></h3>
<pre><code class="language-vbnet">Function DarkenColor(color As Long, percent As Double) As Long
    &#x27; Darken a color by a percentage (0-100)
    Dim r As Integer, g As Integer, b As Integer
    r = color And &amp;HFF
    g = (color \ 256) And &amp;HFF
    b = (color \ 65536) And &amp;HFF
    &#x27; Decrease each component toward 0
    r = r - (r * percent / 100)
    g = g - (g * percent / 100)
    b = b - (b * percent / 100)
    DarkenColor = RGB(r, g, b)
End Function</code></pre>
<h3 id="pattern-5-blendcolors">Pattern 5: <code>BlendColors</code></h3>
<pre><code class="language-vbnet">Function BlendColors(color1 As Long, color2 As Long, _
                    Optional ratio As Double = 0.5) As Long
    &#x27; Blend two colors together (ratio 0.0 = color1, 1.0 = color2)
    Dim r1 As Integer, g1 As Integer, b1 As Integer
    Dim r2 As Integer, g2 As Integer, b2 As Integer
    Dim r As Integer, g As Integer, b As Integer
    &#x27; Extract components
    r1 = color1 And &amp;HFF
    g1 = (color1 \ 256) And &amp;HFF
    b1 = (color1 \ 65536) And &amp;HFF
    r2 = color2 And &amp;HFF
    g2 = (color2 \ 256) And &amp;HFF
    b2 = (color2 \ 65536) And &amp;HFF
    &#x27; Blend
    r = r1 + ((r2 - r1) * ratio)
    g = g1 + ((g2 - g1) * ratio)
    b = b1 + ((b2 - b1) * ratio)
    BlendColors = RGB(r, g, b)
End Function</code></pre>
<h3 id="pattern-6-colorfromhex">Pattern 6: <code>ColorFromHex</code></h3>
<pre><code class="language-vbnet">Function ColorFromHex(hexColor As String) As Long
    &#x27; Convert hex string like &quot;#FF0000&quot; to RGB color
    Dim r As Integer, g As Integer, b As Integer
    &#x27; Remove # if present
    If Left(hexColor, 1) = &quot;#&quot; Then
        hexColor = Mid(hexColor, 2)
    End If
    &#x27; Extract components (assumes format RRGGBB)
    r = Val(&quot;&amp;H&quot; &amp; Mid(hexColor, 1, 2))
    g = Val(&quot;&amp;H&quot; &amp; Mid(hexColor, 3, 2))
    b = Val(&quot;&amp;H&quot; &amp; Mid(hexColor, 5, 2))
    ColorFromHex = RGB(r, g, b)
End Function</code></pre>
<h3 id="pattern-7-colortohex">Pattern 7: <code>ColorToHex</code></h3>
<pre><code class="language-vbnet">Function ColorToHex(color As Long) As String
    &#x27; Convert RGB color to hex string
    Dim r As Integer, g As Integer, b As Integer
    r = color And &amp;HFF
    g = (color \ 256) And &amp;HFF
    b = (color \ 65536) And &amp;HFF
    ColorToHex = &quot;#&quot; &amp; _
                 Right(&quot;0&quot; &amp; Hex(r), 2) &amp; _
                 Right(&quot;0&quot; &amp; Hex(g), 2) &amp; _
                 Right(&quot;0&quot; &amp; Hex(b), 2)
End Function</code></pre>
<h3 id="pattern-8-getcontrastcolor">Pattern 8: <code>GetContrastColor</code></h3>
<pre><code class="language-vbnet">Function GetContrastColor(bgColor As Long) As Long
    &#x27; Return black or white for best contrast
    Dim r As Integer, g As Integer, b As Integer
    Dim luminance As Double
    r = bgColor And &amp;HFF
    g = (bgColor \ 256) And &amp;HFF
    b = (bgColor \ 65536) And &amp;HFF
    &#x27; Calculate perceived luminance
    luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255
    If luminance &gt; 0.5 Then
        GetContrastColor = RGB(0, 0, 0)      &#x27; Black
    Else
        GetContrastColor = RGB(255, 255, 255) &#x27; White
    End If
End Function</code></pre>
<h3 id="pattern-9-temperaturetocolor">Pattern 9: <code>TemperatureToColor</code></h3>
<pre><code class="language-vbnet">Function TemperatureToColor(temp As Double, minTemp As Double, _
                           maxTemp As Double) As Long
    &#x27; Map temperature to color (blue = cold, red = hot)
    Dim ratio As Double
    Dim r As Integer, g As Integer, b As Integer
    &#x27; Normalize temperature to 0-1 range
    ratio = (temp - minTemp) / (maxTemp - minTemp)
    If ratio &lt; 0 Then ratio = 0
    If ratio &gt; 1 Then ratio = 1
    If ratio &lt; 0.5 Then
        &#x27; Blue to cyan to green
        r = 0
        g = ratio * 2 * 255
        b = 255 - (ratio * 2 * 255)
    Else
        &#x27; Green to yellow to red
        r = (ratio - 0.5) * 2 * 255
        g = 255 - ((ratio - 0.5) * 2 * 255)
        b = 0
    End If
    TemperatureToColor = RGB(r, g, b)
End Function</code></pre>
<h3 id="pattern-10-invertcolor">Pattern 10: <code>InvertColor</code></h3>
<pre><code class="language-vbnet">Function InvertColor(color As Long) As Long
    &#x27; Invert a color (negative)
    Dim r As Integer, g As Integer, b As Integer
    r = color And &amp;HFF
    g = (color \ 256) And &amp;HFF
    b = (color \ 65536) And &amp;HFF
    InvertColor = RGB(255 - r, 255 - g, 255 - b)
End Function</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="example-1-color-palette-manager">Example 1: Color Palette Manager</h3>
<pre><code class="language-vbnet">&#x27; Comprehensive color palette management system
Class ColorPalette
    Private m_colors() As Long
    Private m_colorNames() As String
    Private m_count As Integer
    Public Sub Initialize()
        m_count = 0
        ReDim m_colors(0 To 99)
        ReDim m_colorNames(0 To 99)
        LoadDefaultColors
    End Sub
    Private Sub LoadDefaultColors()
        AddColor &quot;Red&quot;, RGB(255, 0, 0)
        AddColor &quot;Green&quot;, RGB(0, 255, 0)
        AddColor &quot;Blue&quot;, RGB(0, 0, 255)
        AddColor &quot;Yellow&quot;, RGB(255, 255, 0)
        AddColor &quot;Magenta&quot;, RGB(255, 0, 255)
        AddColor &quot;Cyan&quot;, RGB(0, 255, 255)
        AddColor &quot;White&quot;, RGB(255, 255, 255)
        AddColor &quot;Black&quot;, RGB(0, 0, 0)
        AddColor &quot;Gray&quot;, RGB(128, 128, 128)
        AddColor &quot;Orange&quot;, RGB(255, 165, 0)
        AddColor &quot;Purple&quot;, RGB(128, 0, 128)
        AddColor &quot;Brown&quot;, RGB(165, 42, 42)
    End Sub
    Public Sub AddColor(name As String, color As Long)
        If m_count &gt; UBound(m_colors) Then
            ReDim Preserve m_colors(0 To UBound(m_colors) + 50)
            ReDim Preserve m_colorNames(0 To UBound(m_colorNames) + 50)
        End If
        m_colorNames(m_count) = name
        m_colors(m_count) = color
        m_count = m_count + 1
    End Sub
    Public Function GetColor(name As String) As Long
        Dim i As Integer
        For i = 0 To m_count - 1
            If UCase(m_colorNames(i)) = UCase(name) Then
                GetColor = m_colors(i)
                Exit Function
            End If
        Next i
        GetColor = RGB(0, 0, 0)  &#x27; Default to black
    End Function
    Public Function CreateGradientPalette(startColor As Long, endColor As Long, _
                                         steps As Integer) As Long()
        Dim palette() As Long
        Dim i As Integer
        Dim r1 As Integer, g1 As Integer, b1 As Integer
        Dim r2 As Integer, g2 As Integer, b2 As Integer
        Dim r As Integer, g As Integer, b As Integer
        Dim ratio As Double
        ReDim palette(0 To steps - 1)
        &#x27; Extract components
        r1 = startColor And &amp;HFF
        g1 = (startColor \ 256) And &amp;HFF
        b1 = (startColor \ 65536) And &amp;HFF
        r2 = endColor And &amp;HFF
        g2 = (endColor \ 256) And &amp;HFF
        b2 = (endColor \ 65536) And &amp;HFF
        For i = 0 To steps - 1
            ratio = i / (steps - 1)
            r = r1 + ((r2 - r1) * ratio)
            g = g1 + ((g2 - g1) * ratio)
            b = b1 + ((b2 - b1) * ratio)
            palette(i) = RGB(r, g, b)
        Next i
        CreateGradientPalette = palette
    End Function
    Public Function GetColorCount() As Integer
        GetColorCount = m_count
    End Function
    Public Function GetColorByIndex(index As Integer) As Long
        If index &gt;= 0 And index &lt; m_count Then
            GetColorByIndex = m_colors(index)
        Else
            GetColorByIndex = RGB(0, 0, 0)
        End If
    End Function
    Public Function GetNameByIndex(index As Integer) As String
        If index &gt;= 0 And index &lt; m_count Then
            GetNameByIndex = m_colorNames(index)
        Else
            GetNameByIndex = &quot;&quot;
        End If
    End Function
End Class</code></pre>
<h3 id="example-2-heat-map-generator">Example 2: Heat Map Generator</h3>
<pre><code class="language-vbnet">&#x27; Generate heat map colors for data visualization
Module HeatMapColors
    Public Function GetHeatMapColor(value As Double, minValue As Double, _
                                   maxValue As Double) As Long
        &#x27; Returns color from blue (cold) to red (hot)
        Dim ratio As Double
        Dim r As Integer, g As Integer, b As Integer
        &#x27; Normalize value to 0-1 range
        If maxValue = minValue Then
            ratio = 0.5
        Else
            ratio = (value - minValue) / (maxValue - minValue)
        End If
        &#x27; Clamp ratio to 0-1
        If ratio &lt; 0 Then ratio = 0
        If ratio &gt; 1 Then ratio = 1
        &#x27; Calculate color based on ratio
        If ratio &lt; 0.25 Then
            &#x27; Blue to cyan
            r = 0
            g = ratio * 4 * 255
            b = 255
        ElseIf ratio &lt; 0.5 Then
            &#x27; Cyan to green
            r = 0
            g = 255
            b = 255 - ((ratio - 0.25) * 4 * 255)
        ElseIf ratio &lt; 0.75 Then
            &#x27; Green to yellow
            r = (ratio - 0.5) * 4 * 255
            g = 255
            b = 0
        Else
            &#x27; Yellow to red
            r = 255
            g = 255 - ((ratio - 0.75) * 4 * 255)
            b = 0
        End If
        GetHeatMapColor = RGB(r, g, b)
    End Function
    Public Sub ApplyHeatMapToRange(dataRange() As Double, controls() As Control)
        Dim i As Integer
        Dim minVal As Double, maxVal As Double
        &#x27; Find min and max values
        minVal = dataRange(LBound(dataRange))
        maxVal = dataRange(LBound(dataRange))
        For i = LBound(dataRange) To UBound(dataRange)
            If dataRange(i) &lt; minVal Then minVal = dataRange(i)
            If dataRange(i) &gt; maxVal Then maxVal = dataRange(i)
        Next i
        &#x27; Apply colors
        For i = LBound(dataRange) To UBound(dataRange)
            controls(i).BackColor = GetHeatMapColor(dataRange(i), minVal, maxVal)
            controls(i).ForeColor = GetContrastColor(controls(i).BackColor)
        Next i
    End Sub
    Private Function GetContrastColor(bgColor As Long) As Long
        Dim r As Integer, g As Integer, b As Integer
        Dim luminance As Double
        r = bgColor And &amp;HFF
        g = (bgColor \ 256) And &amp;HFF
        b = (bgColor \ 65536) And &amp;HFF
        luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255
        If luminance &gt; 0.5 Then
            GetContrastColor = RGB(0, 0, 0)
        Else
            GetContrastColor = RGB(255, 255, 255)
        End If
    End Function
End Module</code></pre>
<h3 id="example-3-theme-manager">Example 3: Theme Manager</h3>
<pre><code class="language-vbnet">&#x27; Application theme management with color schemes
Class ThemeManager
    Private Type ColorScheme
        Background As Long
        Foreground As Long
        Accent As Long
        Highlight As Long
        Border As Long
    End Type
    Private m_currentTheme As ColorScheme
    Private m_themeName As String
    Public Sub SetLightTheme()
        m_themeName = &quot;Light&quot;
        With m_currentTheme
            .Background = RGB(255, 255, 255)    &#x27; White
            .Foreground = RGB(0, 0, 0)          &#x27; Black
            .Accent = RGB(0, 120, 215)          &#x27; Blue
            .Highlight = RGB(255, 255, 0)       &#x27; Yellow
            .Border = RGB(192, 192, 192)        &#x27; Light Gray
        End With
    End Sub
    Public Sub SetDarkTheme()
        m_themeName = &quot;Dark&quot;
        With m_currentTheme
            .Background = RGB(32, 32, 32)       &#x27; Dark Gray
            .Foreground = RGB(255, 255, 255)    &#x27; White
            .Accent = RGB(0, 120, 215)          &#x27; Blue
            .Highlight = RGB(255, 200, 0)       &#x27; Gold
            .Border = RGB(64, 64, 64)           &#x27; Medium Gray
        End With
    End Sub
    Public Sub SetCustomTheme(bg As Long, fg As Long, accent As Long, _
                             highlight As Long, border As Long)
        m_themeName = &quot;Custom&quot;
        With m_currentTheme
            .Background = bg
            .Foreground = fg
            .Accent = accent
            .Highlight = highlight
            .Border = border
        End With
    End Sub
    Public Sub ApplyToForm(frm As Form)
        Dim ctrl As Control
        frm.BackColor = m_currentTheme.Background
        frm.ForeColor = m_currentTheme.Foreground
        For Each ctrl In frm.Controls
            ApplyToControl ctrl
        Next ctrl
    End Sub
    Private Sub ApplyToControl(ctrl As Control)
        On Error Resume Next
        &#x27; Apply based on control type
        Select Case TypeName(ctrl)
            Case &quot;TextBox&quot;
                ctrl.BackColor = m_currentTheme.Background
                ctrl.ForeColor = m_currentTheme.Foreground
            Case &quot;CommandButton&quot;
                ctrl.BackColor = m_currentTheme.Accent
                ctrl.ForeColor = RGB(255, 255, 255)
            Case &quot;Label&quot;
                ctrl.BackColor = m_currentTheme.Background
                ctrl.ForeColor = m_currentTheme.Foreground
            Case &quot;ListBox&quot;, &quot;ComboBox&quot;
                ctrl.BackColor = m_currentTheme.Background
                ctrl.ForeColor = m_currentTheme.Foreground
        End Select
        On Error GoTo 0
    End Sub
    Public Function GetBackgroundColor() As Long
        GetBackgroundColor = m_currentTheme.Background
    End Function
    Public Function GetForegroundColor() As Long
        GetForegroundColor = m_currentTheme.Foreground
    End Function
    Public Function GetAccentColor() As Long
        GetAccentColor = m_currentTheme.Accent
    End Function
    Public Function GetThemeName() As String
        GetThemeName = m_themeName
    End Function
End Class</code></pre>
<h3 id="example-4-chart-color-generator">Example 4: Chart Color Generator</h3>
<pre><code class="language-vbnet">&#x27; Generate distinct colors for charts and graphs
Class ChartColorGenerator
    Private m_baseHue As Integer
    Private m_colorIndex As Integer
    Public Sub Initialize(Optional startHue As Integer = 0)
        m_baseHue = startHue
        m_colorIndex = 0
    End Sub
    Public Function GetNextColor() As Long
        Dim hue As Integer
        Dim r As Integer, g As Integer, b As Integer
        &#x27; Use golden angle for good color distribution
        hue = (m_baseHue + (m_colorIndex * 137)) Mod 360
        m_colorIndex = m_colorIndex + 1
        GetNextColor = HSVToRGB(hue, 0.7, 0.9)
    End Function
    Private Function HSVToRGB(h As Double, s As Double, v As Double) As Long
        &#x27; Convert HSV to RGB
        Dim r As Double, g As Double, b As Double
        Dim i As Integer
        Dim f As Double, p As Double, q As Double, t As Double
        If s = 0 Then
            r = v: g = v: b = v
        Else
            h = h / 60
            i = Int(h)
            f = h - i
            p = v * (1 - s)
            q = v * (1 - s * f)
            t = v * (1 - s * (1 - f))
            Select Case i
                Case 0: r = v: g = t: b = p
                Case 1: r = q: g = v: b = p
                Case 2: r = p: g = v: b = t
                Case 3: r = p: g = q: b = v
                Case 4: r = t: g = p: b = v
                Case Else: r = v: g = p: b = q
            End Select
        End If
        HSVToRGB = RGB(r * 255, g * 255, b * 255)
    End Function
    Public Sub Reset()
        m_colorIndex = 0
    End Sub
    Public Function GetColorArray(count As Integer) As Long()
        Dim colors() As Long
        Dim i As Integer
        ReDim colors(0 To count - 1)
        Reset
        For i = 0 To count - 1
            colors(i) = GetNextColor()
        Next i
        GetColorArray = colors
    End Function
End Class</code></pre>
<h2 id="error-handling">Error Handling</h2>
<p>The <code>RGB</code> function automatically handles out-of-range values:
- Values greater than 255 are treated as 255 (saturated)
- Negative values are treated as 0
- Non-integer values are rounded to integers
- No error is raised for out-of-range values
The function is very robust and rarely requires error handling:</p>
<pre><code class="language-vbnet">&#x27; RGB automatically clamps values to valid range
Dim color As Long
color = RGB(300, -50, 150)  &#x27; Treated as RGB(255, 0, 150)</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li>The <code>RGB</code> function is extremely fast - it's a simple calculation</li>
<li>Can be called thousands of times with negligible performance impact</li>
<li>No need to cache RGB values unless doing complex color calculations</li>
<li>Extracting components from an RGB value is slightly slower than creating it</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Use Named Constants</strong>: Define color constants for reusability</li>
<li><strong>Document Color Choices</strong>: Comment why specific colors were chosen</li>
<li><strong>Consider Accessibility</strong>: Ensure sufficient contrast for readability</li>
<li><strong>Test on Different Displays</strong>: Colors may appear different on various monitors</li>
<li><strong>Use Color Schemes</strong>: Create coordinated palettes rather than random colors</li>
<li><strong>Extract Components Carefully</strong>: Remember BGR byte order when extracting</li>
<li><strong>Validate User Input</strong>: When accepting color values from users</li>
<li><strong>Use Gradients Wisely</strong>: Smooth gradients are more visually appealing</li>
<li><strong>Consider Color Blindness</strong>: Test with color-blind-friendly palettes</li>
<li><strong>Avoid Magic Numbers</strong>: Use <code>RGB()</code> rather than numeric color values</li>
</ol>
<h2 id="comparison-with-related-functions">Comparison with Related Functions</h2>
<table>
<thead>
<tr>
<th>Function</th>
<th>Purpose</th>
<th>Returns</th>
<th>Use Case</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>RGB</strong></td>
<td>Create custom color</td>
<td>Long (RGB value)</td>
<td>Precise color control, custom colors</td>
</tr>
<tr>
<td><strong><code>QBColor</code></strong></td>
<td>Get <code>QBasic</code> color</td>
<td>Long (RGB value)</td>
<td>Legacy compatibility, 16-color palette</td>
</tr>
<tr>
<td><strong>vbRed, vbBlue, etc.</strong></td>
<td>Predefined constants</td>
<td>Long (RGB value)</td>
<td>Quick standard colors</td>
</tr>
<tr>
<td><strong><code>LoadPicture</code></strong></td>
<td>Load image</td>
<td>Picture object</td>
<td>Complex graphics, images</td>
</tr>
</tbody>
</table>
<h2 id="platform-and-version-notes">Platform and Version Notes</h2>
<ul>
<li>Available in all versions of VB6 and VBA</li>
<li>Behavior consistent across all Windows platforms</li>
<li>Returns BGR byte order (standard for Windows)</li>
<li>Color values are compatible with Windows API functions</li>
<li>Maximum value is &amp;HFFFFFF (16,777,215 colors)</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Limited to 24-bit color (16.7 million colors)</li>
<li>No alpha channel support (transparency)</li>
<li>Automatic clamping may mask input errors</li>
<li>BGR byte order can be confusing when extracting components</li>
<li>No built-in color space conversions (HSV, HSL, etc.)</li>
<li>No color validation or named color lookup</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>QBColor</code>: Returns RGB value for <code>QBasic</code> color number (0-15)</li>
<li>Predefined color constants: <code>vbBlack</code>, <code>vbRed</code>, <code>vbGreen</code>, <code>vbBlue</code>, <code>vbWhite</code>, etc.</li>
<li><code>LoadPicture</code>: Loads images with full color support</li>
<li><code>Point</code>: Returns the RGB color of a specified point on a form or picture box</li>
</ul>
        </article>
        
        <div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
            <p>
                <a href="index.html">← Back to Graphics</a> |
                <a href="../index.html">View all functions</a>
            </p>
        </div>

    </main>

    <footer>
        <div class="container">
            <p>&copy; 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
        </div>
    </footer>
</body>
</html>