![]() |
#8
|
||||
|
||||
![]()
Using the help from this thread, I added a new feature to my random color macro. The code snippet below will assign a random color from the entire color palette (16M colors) to each character in the selection. The result is kinda cool.
Code:
Sub MyRandCharColors() Dim oChr As Range Dim lngR As Long, lngG As Long, lngB As Long Randomize For Each oChr In Selection.Characters lngR = Rnd() * 256 lngG = Rnd() * 256 lngB = Rnd() * 256 oChr.Font.Color = RGB(lngR, lngG, lngB) Next oChr End Sub Code:
oChr.Font.Color = RGB(lngR, lngR, lngR) '256 shades of grey Should the Rnd() factor be 256 or 255? Rnd() returns a number on [0,1). That is, including 0, but not 1. If I use 255, I'll never get a 255 color value. Right? Should I convert lngR et al to integers (int(lngR))? It seems to work as is. Some of the colors this macro produces are too light. The next snippet makes an adjustment if the sum of the color values exceed some threshold. This seems to work pretty well. Code:
Sub MyRandCharColors() Dim oChr As Range Dim lngR As Long, lngG As Long, lngB As Long Dim lngRGBSum As Long, lngRGBF As Long Const lngRGBMax As Long = 500 Randomize For Each oChr In Selection.Characters lngR = Rnd() * 256 lngG = Rnd() * 256 lngB = Rnd() * 256 lngRGBSum = lngR + lngG + lngB If lngRGBSum > lngRGBMax Then 'If sum > max, scale it down to max lngRGBF = lngRGBMax / lngRGBSum lngR = lngR * lngRGBF lngG = lngG * lngRGBF lngB = lngB * lngRGBF End If oChr.Font.Color = RGB(lngR, lngG, lngB) Next oChr End Sub |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
tinfanide | Excel Programming | 2 | 06-10-2012 10:17 AM |
![]() |
scrinmemphis | Word | 4 | 06-06-2012 11:50 PM |
![]() |
joatmon | Excel Programming | 1 | 05-30-2012 08:23 PM |
Help with VBA macro - Variable input | sc30317 | Excel Programming | 0 | 08-31-2011 01:00 PM |
Variable fields? | Emalee77 | PowerPoint | 0 | 01-30-2011 05:58 PM |