Author: Jeff Hobbs.
Converts a color specified as Hue, Saturation and Value into an RGB triple. The result can be processed by "dec2rgb" (See recipe "Color Manipulation").
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 | # hsv2rgb --
#
# Convert hsv to rgb
#
# Arguments:
# h hue
# s saturation
# v value
# Results:
# Returns an rgb triple from hsv
#
proc hsv2rgb {h s v} {
if {$s <= 0.0} {
# achromatic
set v [expr {int($v)}]
return "$v $v $v"
} else {
set v [expr {double($v)}]
if {$h >= 1.0} { set h 0.0 }
set h [expr {6.0 * $h}]
set f [expr {double($h) - int($h)}]
set p [expr {int(256 * $v * (1.0 - $s))}]
set q [expr {int(256 * $v * (1.0 - ($s * $f)))}]
set t [expr {int(256 * $v * (1.0 - ($s * (1.0 - $f))))}]
set v [expr {int(256 * $v)}]
switch [expr {int($h)}] {
0 { return "$v $t $p" }
1 { return "$q $v $p" }
2 { return "$p $v $t" }
3 { return "$p $q $v" }
4 { return "$t $p $v" }
5 { return "$v $p $q" }
}
}
}
|
If you're looking to convert colorspaces, there is a python builtin module colorsys that does that stuff: http://aspn.activestate.com/ASPN/docs/ActivePython/2.4/python/lib/module-colorsys.html
Also, there is a recipe for RGB to hex/HTML colors: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/266466
whoops. i'm not trying to convert anyone to python, that comment was meant for this page:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/266466
The above code gives problems when trying to convert to the hexadecimal RGB notation (e.g. #123456) when value equals 1. Also, if saturation equals 0, then the value should be multiplied by 255, like it is for saturation greater than 0. Herewith the modified code: