## Picking a range of colors

DKF: I wanted to pick a range of 9 colours (for indicating the priorities of Tcl bug reports , though you only get meaningful results there if you're a Tcl developer) yet the RGB color space is a tricky thing to work with. I wanted something like the range used by the old SourceForge tracker system, which ranged (approximately) from red through to purple. Getting a good perceptual sweep is non-trivial, but when dealing with how people perceive colors, a good rule is to use HSV rather than RGB. In particular, if I use code from HSV and RGB, I can do this:

```set From "#ff462d"
set To "#a394c6"

# Convert to HSV and assign to variables
lassign [rgbToHsv {*}[scan \$From "#%2x%2x%2x"]] from(h) from(s) from(v)
lassign [rgbToHsv {*}[scan \$To "#%2x%2x%2x"]] to(h) to(s) to(v)

# Calculate the size of the steps; we're printing 9 items, so 8 steps
foreach c {h s v} {set step(\$c) [expr {(\$to(\$c) - \$from(\$c)) / 8.}]}

# Initialize the iteration 'variable'
array set col [array get from]

# 9 steps…
for {set i 0} {\$i<9} {incr i} {
# Print…
puts [format #%02x%02x%02x {*}[hsvToRgb \$col(h) \$col(s) \$col(v)]]
# … increment
foreach c {h s v} {set col(\$c) [expr {\$col(\$c) + \$step(\$c)}]}
}```

Which produces this output:

```#ff462d
#f8b53d
#d6f14d
#88ea5b
#69e382
#75dbc0
#81bfd4
#8b9acd
#a394c6```

It's not perfect, as it doesn't take into account the different visual acuity of the human eye in different color channels, but it produces a reasonably pleasing result.

 Category Graphics