corz.org uses cookies to remember that you've seen this notice explaining that corz.org uses cookies, okay!
corz color functions v0.4
miscellaneous functions for manipulating colors and their values
(c) cor + corz.org 2007->tomorrow!
function list..
ConvertColorValue( string{RGB hex color value}, string{output mode} [, boolean{add prefix=false} [, GUI-bool{lower-case]]})
RGBToCMYK( string{hex rgb color[, bool{normalize to percentage}])
RGBToHSL( string{hex rgb color)[, int{index to return}])
RegColorToRGBHexColor()
GetColorRed($nColor)
GetColorGreen($nColor)
GetColorBlue($nColor)
ColorChooser(array {Custom Colors}, HWND {Ownder})
; output. eg, FF00FF becomes
func ConvertColorValue($color, $mode="web", $prefix=false, $index=0, $lowercase=$OFF)
if StringLeft($color, 1) = " then $color = StringTrimLeft($color, 1)
$pre = ""
switch $mode
case "i", "RGB Integer", "RGB Int", "int"
switch $index
case 0
$color = Dec(StringLeft($color, 2)) & "," & Dec(StringMid($color, 3, 2)) & "," & Dec(StringRight($color, 2))
case 1
return Dec(StringLeft($color, 2))
case 2
return Dec(StringMid($color, 3, 2))
case 3
return Dec(StringRight($color, 2))
endswitch
case "a", "Autoit RGB Hex", "Autoit RGB", "AutoIt Hex", "AutoIt"
$color = '0x' & $color
case "b", "Autoit BGR Hex", "Autoit BGR", "BGR Hex", "bgr"
$color = '0x' & StringRight($color, 2) & StringMid($color, 3, 2) & StringLeft($color, 2)
case "d", "Delphi", "Delphi Hex"
$color = "00" & StringRight($color, 2) & StringMid($color, 3, 2) & StringLeft($color, 2)
$pre = "$"
case "v", "vc", "Visual C++ Hex", "Visual C++ BGR", "Visual C++", "Visual C++ BGR Hex", "C++"
$color = "0x00" & StringRight($color, 2) & StringMid($color, 3, 2) & StringLeft($color, 2)
case "RGB Float", "float", "f"
$r = Round((1/255) * Dec(StringLeft($color, 2)), 2)
$g = Round((1/255) * Dec(StringMid($color, 3, 2)), 2)
$b = Round((1/255) * Dec(StringRight($color, 2)),2)
$color = StringFormat("%, $r) & "," & StringFormat("%#.2f", $g) & "," & StringFormat("%#.2f", $b)
case "h", "Hue/Sat/Lum", "HSL", "h/s/l"
$color = RGBToHSL($color, ",", 100)
case "k", "cmyk", "c/m/y/k"
if $prefix = 1 then
$color = RGBToCMYK($color, true)
else
$color = RGBToCMYK($color)
endif
case "w", "Web Hex", "Web", "Hex"
$pre = "
endswitch
if not $prefix or $prefix = 4 then $pre = ""
if $lowercase = $ON then $color = StringLower($color)
return $pre & $color
endfunc
func RGBToCMYK($rgb_color, $norm=0)
$rc_r = ConvertColorValue($rgb_color, "i", 0, 1) / 255
$rc_g = ConvertColorValue($rgb_color, "i", 0, 2) / 255
$rc_b = ConvertColorValue($rgb_color, "i", 0, 3) / 255
$k = MinMin(1-$rc_r, 1-$rc_g, 1-$rc_b)
$c = (1 - $rc_r - $k) / ( 1 - $k)
$m = (1 - $rc_g - $k) / ( 1 - $k)
$y = (1 - $rc_b - $k) / ( 1 - $k)
if $norm then
return Round($c * 100, 1) & "," & Round($m * 100, 1) & "," & Round($y * 100, 1) & "," & Round($k * 100, 1)
else
return Round($c, 3) & "," & Round($m, 3) & "," & Round($y, 3) & "," & Round($k, 3)
endif
endfunc
func RGBToHSL($rgb_color, $idx="", $simple_array=false, $hsl_scale=1)
$rh_r = ConvertColorValue($rgb_color, "i", 0, 1) / 255
$rh_g = ConvertColorValue($rgb_color, "i", 0, 2) / 255
$rh_b = ConvertColorValue($rgb_color, "i", 0, 3) / 255
$rh_min = MinMin($rh_r, $rh_g, $rh_b)
$rh_max = MaxMax($rh_r, $rh_g, $rh_b)
$rh_delta = $rh_max - $rh_min
if $idx <> 1 then
$rh_lightness = ($rh_min + $rh_max) / 2
if $idx = 3 then return Round($rh_lightness*$hsl_scale, 2)
$rh_saturation = 0
if $rh_lightness > 0 and $rh_lightness < 1 then
if $rh_lightness < 0.5 then
$rh_saturation = $rh_delta / (2 * $rh_lightness)
else
$rh_saturation = $rh_delta / (2 - 2 * $rh_lightness)
endif
endif
if $idx = 2 then return Round($rh_saturation*$hsl_scale, 2)
endif
$rh_hue = 0
if $rh_delta > 0 then
if $rh_max = $rh_r and $rh_max <> $rh_g then
$rh_hue += ($rh_g - $rh_b) / $rh_delta
endif
if $rh_max = $rh_g and $rh_max <> $rh_b then
$rh_hue += 2 + ($rh_b - $rh_r) / $rh_delta
endif
if $rh_max = $rh_b and $rh_max <> $rh_r then
$rh_hue += 4 + ($rh_r - $rh_g) / $rh_delta
endif
$rh_hue *= 60
endif
if $rh_hue < 0 then $rh_hue += 360
if $idx = 1 then return Round($rh_hue)
$do_string = true
if not $idx then
$idx = ","
$do_string = false
endif
local $hsl_arr[3]
$hsl_arr[0] = Round($rh_hue)
$hsl_arr[1] = Round($rh_saturation*$hsl_scale, 2)
$hsl_arr[2] = Round($rh_lightness*$hsl_scale, 2)
$hsl = $hsl_arr[0] & $idx & $hsl_arr[1] & $idx & $hsl_arr[2]
if $do_string then return $hsl
if $simple_array then return $hsl_arr
return StringSplit($hsl, $idx)
endfunc
func RegColorToRGBHexColor($RegVal)
$col = StringSplit($RegVal, " ")
if $col[0] < 2 then return ""
return Hex($col[1], 2) & Hex($col[2], 2) & Hex($col[3], 2)
endfunc
func ConvertRGBtoDecArray($color)
local $c_arr[3]
$c_arr[0] = Dec(StringLeft($color, 2))
$c_arr[1] = Dec(StringMid ($color, 3 , 2))
$c_arr[2] = Dec(StringRight($color, 2))
return $c_arr
endfunc
Func GetColorRed($nColor)
Return BitAND(BitShift($nColor, 16), 0xff)
EndFunc
Func GetColorGreen($nColor)
Return BitAND(BitShift($nColor, 8), 0xff)
EndFunc
Func GetColorBlue($nColor)
Return BitAND($nColor, 0xff)
EndFunc
func ColorChooser($CustomColors=0, $hWndOwnder=0)
if not IsArray($CustomColors) or Ubound($CustomColors) < 17 then
$CustomColors = StringSplit("0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0", ",", 2)
endif
local $ret_array[17], $color_picked, $aResult
local $custcolors = "int ccolors[16]"
local $col_STRUCT = "dword Size
local $tChoose = DllStructCreate($col_STRUCT)
local $tcc = DllStructCreate($custcolors)
$CustomColors[0] = '0x' & StringMid($CustomColors[0], 7, 2) & StringMid($CustomColors[0], 5, 2) & StringMid($CustomColors[0], 3, 2)
DllStructSetData($tChoose, "Size", DllStructGetSize($tChoose))
DllStructSetData($tChoose, "hWndOwnder", $hWndOwnder)
DllStructSetData($tChoose, "rgbResult", $CustomColors[0])
DllStructSetData($tChoose, "CustColors", DllStructGetPtr($tcc))
DllStructSetData($tChoose, "Flags", BitOR($__MISCCONSTANT_CC_ANYCOLOR, $__MISCCONSTANT_CC_FULLOPEN, $__MISCCONSTANT_CC_RGBINIT))
for $i = 1 to 16
$ccolor = Dec(StringMid($CustomColors[$i], 7, 2) & StringMid($CustomColors[$i], 5, 2) & StringMid($CustomColors[$i], 3, 2))
DllStructSetData($tcc, "ccolors", $ccolor, $i)
next
$aResult = DllCall("comdlg32.dll", "bool", "ChooseColor", "struct*", $tChoose)
if ($aResult[0] == 0) then return SetError(-3, -3, -1)
if @error then return SetError(@error, @extended, -1)
$color_picked = Hex(String(DllStructGetData($tChoose, "rgbResult")), 6)
if $color_picked < 0 then return SetError(-4, -4, -1)
$ret_array[0] = '0x' & StringMid($color_picked, 5, 2) & StringMid($color_picked, 3, 2) & StringMid($color_picked, 1, 2)
for $i = 1 to 16
$cc = Hex(DllStructGetData($tcc, "ccolors", $i), 6)
$ret_array[$i] = '0x' & StringMid($cc, 5, 2) & StringMid($cc, 3, 2) & StringMid($cc, 1, 2)
next
return $ret_array
endfunc
global $MyColors[17] = [""]
func HotKeySystemPicker()
SystemPicker($current_color)
endfunc
func SystemPicker($color)
UnSetHotKeys()
LoadCustomColors()
$MyColors[0] = '0x' & $color
$ret_colors = ColorChooser($MyColors, $MyGUI)
if @error then return false
switch $ret_colors[0]
case -1, -3, -4, ""
SetHotKeys()
return false
case else
$MyColors = $ret_colors
$new_color = StringTrimLeft($MyColors[0], 2)
SaveCustomColors()
SetHotKeys()
endswitch
endfunc
func SaveCustomColors()
if not IsArray($MyColors) or UBound($MyColors) < 17 then return false
local $color_str
for $i = 1 to 16
$color_str &= $MyColors[$i] & ","
next
$color_str = StringTrimRight($color_str, 1)
if IniWrite($cpc_ini_path, $cpc_my_name, "custom_colors", $color_str) then return true
endfunc
func LoadCustomColors()
local $color_str = IniRead($cpc_ini_path, $cpc_my_name, "custom_colors", "")
$MyColors = StringSplit($color_str, ",")
endfunc