The following function is a representation of a formula I’ve worked on-and-off with for the past several years. I often like to represent a value with a “common” temperature gradient which goes gradually from blue through green and yellow to eventually red. In short, something like this:
Or, representing percent in steps of 10 as an example:
0 | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 |
As it would turn out, this isn’t as simple as it first seems. Ultimately, I ended up with the following 3 distinct gradient calculations (values going from 0-255)
First third: starting with a blue RGB of 64,128,255, gradually increase Green and reduce Blue.
Second third: gradually increase Red, keep Green steady, and phase out Blue.
Third third: gradually phase out Green, leaving just Red.
I’ve written 3 functions in the following code section. The first one returns an object with the separate RGB components; the second returns the hex code value; the third is a quick way of generating a reference bar (such as the two I’ve included above).
Function TemperatureColor([double]$percent) { [int]$weight=($percent/100.0)*768.0 $color=New-Object PSObject -Property @{Red=255;Green=255;Blue=255} if($weight -ge 768) { $Color.Red=255; $Color.Green=0; $Color.Blue=0 } elseif($Weight -ge 512) { $Color.Red=255; $Color.Green=768-$weight; $Color.Blue=0 } elseif($Weight -ge 256) { $Color.Red=($weight-256)*0.75+64; $Color.Green=255; $Color.Blue=255-($weight/2) } elseif($Weight -ge 0) { $Color.Red=64; $Color.Green=128+($weight/2); $Color.Blue=255-($weight/2) } else { $Color.Red=255; $Color.Green=255; $Color.Blue=255 } $Color.Red=[int]$Color.Red $Color.Green=[int]$Color.Green $Color.Blue=[int]$Color.Blue $Color } Function HexColor($Percent) { $Color=TemperatureColor($Percent) "{0:x2}{1:x2}{2:x2}" -f $Color.Red,$Color.Green,$Color.Blue } Function TemperatureScaleTable { [CmdletBinding()] Param( [Parameter(Mandatory=$false,Position=0)][double]$Step=5, [Parameter(Mandatory=$false,Position=1)][Switch]$Reverse=$false, [Parameter(Mandatory=$false,Position=2)][Switch]$NoValue, [Parameter(Mandatory=$false,Position=3)][Switch]$NoBorder, [Parameter(Mandatory=$false,Position=4)][int]$CellWidth=5 ) if($NoBorder) { $html="<TABLE style=""border: 0px; border-collapse: collapse;"">" } else { $html="<TABLE>" } $html+="<TR style=""height: 20px"">" For($i=0;$i -le 100; $i+=$step) { $v=$i $width="" if($Reverse) { $v=100-$i } if($NoValue) { $v=" ";$width="width: $($CellWidth)px" } $html+="<td style=""background-color: #$(HexColor($i)); $width"">$v</td>" } $html+="</TR></TABLE>" $html | Out-String }
The two bars above were generated with the following code:
TemperatureScaleTable -Step 1 -CellWidth 1 -NoValue -NoBorder TemperatureScaleTable -Step 10
The parameters for TemperatureScaleTable let you change the number of values and even reverses those values (if you want 100=blue and 0=red).
In my next post, I’ll show you how to make use of this function to create a color-coded disk volume report that quickly shows you your “hot spots” where you may be low on disk space.