Jump to content



Photo

Convert RGB to HSB


  • Please log in to reply
14 replies to this topic

#1 weaponx

weaponx

    I'm coming for blood, no code of conduct, no law.

  • MVPs
  • 5,366 posts

Posted 05 February 2008 - 04:34 PM

AutoIt         
#cs ----------------------------------------------------------------------------  AutoIt Version: 3.2.10.0  Author: WeaponX  Script Function:     Convert RGB color to HSB (like Photoshop)    Converted from: [url="http://www.oooforum.org/forum/viewtopic.phtml?t=4945"]http://www.oooforum.org/forum/viewtopic.phtml?t=4945[/url] #ce ---------------------------------------------------------------------------- $result = _RGBtoHSB(255, 000, 000) MsgBox(0, "Red", "H:" & $result[0] & @CRLF & "S:" & $result[1] & @CRLF & "B:" & $result[2]) $result = _RGBtoHSB(000, 255, 000) MsgBox(0, "Green", "H:" & $result[0] & @CRLF & "S:" & $result[1] & @CRLF & "B:" & $result[2]) $result = _RGBtoHSB(000, 000, 255) MsgBox(0, "Blue", "H:" & $result[0] & @CRLF & "S:" & $result[1] & @CRLF & "B:" & $result[2]) ;Input ;$nRed 0-255 ;$nGreen 0-255 ;$nBlue 0-255 ;Returns 3-Element array ;H:[0] 0-360 ;S:[1] 0-100% ;B:[2] 0-100% Func _RGBtoHSB($nRed, $nGreen, $nBlue)     Local $nHue, $nSaturation, $nBrightness, $largest     Local $hsbArray[3] = [0, 0, 0]         ;Determine highest component value     $nMax = 0         If $nRed > $nMax Then $nMax = $nRed     If $nGreen > $nMax Then $nMax = $nGreen     If $nBlue > $nMax Then $nMax = $nBlue         ;Determine lowest component value     $nMin = 255     If $nRed < $nMin Then $nMin = $nRed     If $nGreen < $nMin Then $nMin = $nGreen     If $nBlue < $nMin Then $nMin = $nBlue     If $nMin = $nMax Then         ;Grayscale         $nHue = 0.0         $nSaturation = 0.0         $nBrightness = $nMax     Else         If $nRed = $nMin Then             $d = $nGreen - $nBlue             $h = 3.0         ElseIf $nGreen = $nMin Then             $d = $nBlue - $nRed             $h = 5.0         Else             $d = $nRed - $nGreen             $h = 1.0         EndIf         $nHue = ($h - ($d / ($nMax - $nMin))) / 6.0         $nSaturation = ($nMax - $nMin) / $nMax         $nBrightness = $nMax / 255.0     EndIf     $hsbArray[0] = $nHue * 360     $hsbArray[1] = $nSaturation * 100     $hsbArray[2] = $nBrightness * 100         Return $hsbArray EndFunc   ;==>_RGBtoHSB

Edited by weaponx, 05 February 2008 - 04:35 PM.








#2 XxXFaNtA

XxXFaNtA

    Adventurer

  • Active Members
  • PipPip
  • 128 posts

Posted 05 February 2008 - 05:02 PM

Nice :)

Actually that was what I've been searching here some weeks ago and even wanted to make it on my own..

But now you did the job for me ^^
Thanks!
/

Posted Image


#3 ptrex

ptrex

    Universalist

  • MVPs
  • 2,400 posts

Posted 05 February 2008 - 05:13 PM

@weaponx

Not using Photshop yet, but might come in handy later :)

Thanks

regards

ptrex

#4 -Ultima-

-Ultima-

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 267 posts

Posted 05 February 2008 - 05:19 PM

Hah, nice stuff weaponx :) Interestingly, I just recently created a pair of functions to convert back and forth between RGB and HLS/HSB as well:

[SNIPPED]

These functions modify the passed array ByRef, which is ugly (I was too lazy to change it, and the behavior suited my needs when I created the function). It was a straight port form the Microsoft Knowledge Base article (KB29240) without much afterthought put into it, so it might not be as polished as it can probably be. The translation isn't perfect either (there's a bit of guesstimation going on), so there might be some discrepancies when converting back and forth and back again. The general algorithm is more-or-less there, though :)

See update in later posts...

Edited by -Ultima-, 07 February 2008 - 07:53 PM.

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

#5 -Ultima-

-Ultima-

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 267 posts

Posted 07 February 2008 - 07:54 PM

I've just updated my functions... The algorithm is exactly the same as before, but I looked at easyrgb.com's pseudocode instead to translate. This resulted in much nicer code, but more importantly, it resulted in ACCURATE conversions. At any rate, it's now formatted to UDF standard.
AutoIt         
Global Const $_COLORCONSTANTS_HSLMAX = 240 Global Const $_COLORCONSTANTS_RGBMAX = 255 ; #FUNCTION# ====================================================================================== ; Name...........: _ColorConvertRGBtoHSL ; Description ...: Converts RGB to HSL ; Syntax.........: _ColorConvertRGBtoHSL($avArray) ; Parameters ....: $avArray - An array containing RGB values in their respective positions ; Return values .: Success - The array containing the HSL values for the inputted RGB values ;                  Failure - 0, sets @error to 1 ; Author ........: Ultima ; Modified.......: ; Remarks .......: ; Related .......: _ColorConvertHSLtoRGB ; Link ..........; <a href='http://www.easyrgb.com/math.php?MATH=M18#text18' class='bbc_url' title='External link' rel='nofollow external'>http://www.easyrgb.com/math.php?MATH=M18#text18</a> ; Example .......; ; ================================================================================================= Func _ColorConvertRGBtoHSL($avArray)     If UBound($avArray) <> 3 Or UBound($avArray, 0) <> 1 Then Return SetError(1, 0, 0)     Local $nH, $nS, $nL     Local $nR = Number($avArray[0])/$_COLORCONSTANTS_RGBMAX     Local $nG = Number($avArray[1])/$_COLORCONSTANTS_RGBMAX     Local $nB = Number($avArray[2])/$_COLORCONSTANTS_RGBMAX     Local $nMax = $nR     If $nMax < $nG Then $nMax = $nG     If $nMax < $nB Then $nMax = $nB     Local $nMin = $nR     If $nMin > $nG Then $nMin = $nG     If $nMin > $nB Then $nMin = $nB     Local $nMinMaxSum = ($nMax + $nMin)     Local $nMinMaxDiff = ($nMax - $nMin)     ; Lightness     $nL = $nMinMaxSum/2     If $nMinMaxDiff = 0 Then         ; Grayscale         $nH = 0         $nS = 0     Else         ; Saturation         If $nL < 0.5 Then             $nS = $nMinMaxDiff/$nMinMaxSum         Else             $nS = $nMinMaxDiff/(2 - $nMinMaxSum)         EndIf         ; Hue         Switch $nMax             Case $nR                 $nH = ($nG - $nB)/(6 * $nMinMaxDiff)             Case $nG                 $nH = ($nB - $nR)/(6 * $nMinMaxDiff) + 1/3             Case $nB                 $nH = ($nR - $nG)/(6 * $nMinMaxDiff) + 2/3         EndSwitch         If $nH < 0 Then $nH += 1         If $nH > 1 Then $nH -= 1     EndIf     $avArray[0] = $nH * $_COLORCONSTANTS_HSLMAX     $avArray[1] = $nS * $_COLORCONSTANTS_HSLMAX     $avArray[2] = $nL * $_COLORCONSTANTS_HSLMAX     Return $avArray EndFunc   ;==>_ColorConvertRGBtoHSL ; #FUNCTION# ====================================================================================== ; Name...........: _ColorConvertHSLtoRGB ; Description ...: Converts HSL to RGB ; Syntax.........: _ColorConvertHSLtoRGB($avArray) ; Parameters ....: $avArray - An array containing HSL values in their respective positions ; Return values .: Success - The array containing the RGB values for the inputted HSL values ;                  Failure - 0, sets @error to 1 ; Author ........: Ultima ; Modified.......: ; Remarks .......: ; Related .......: _ColorConvertHSLtoRGB ; Link ..........; <a href='http://www.easyrgb.com/math.php?MATH=M19#text19' class='bbc_url' title='External link' rel='nofollow external'>http://www.easyrgb.com/math.php?MATH=M19#text19</a> ; Example .......; ; ================================================================================================= Func _ColorConvertHSLtoRGB($avArray)     If UBound($avArray) <> 3 Or UBound($avArray, 0) <> 1 Then Return SetError(1, 0, 0)     Local $nR, $nG, $nB     Local $nH = Number($avArray[0])/$_COLORCONSTANTS_HSLMAX     Local $nS = Number($avArray[1])/$_COLORCONSTANTS_HSLMAX     Local $nL = Number($avArray[2])/$_COLORCONSTANTS_HSLMAX     If $nS = 0 Then         ; Grayscale         $nR = $nL         $nG = $nL         $nB = $nL     Else         ; Chromatic         Local $nValA, $nValB         If $nL <= 0.5 Then             $nValB = $nL * ($nS + 1)         Else             $nValB = ($nL + $nS) - ($nL * $nS)         EndIf         $nValA = 2 * $nL - $nValB         $nR = __ColorConvertHueToRGB($nValA, $nValB, $nH + 1/3)         $nG = __ColorConvertHueToRGB($nValA, $nValB, $nH)         $nB = __ColorConvertHueToRGB($nValA, $nValB, $nH - 1/3)     EndIf     $avArray[0] = $nR * $_COLORCONSTANTS_RGBMAX     $avArray[1] = $nG * $_COLORCONSTANTS_RGBMAX     $avArray[2] = $nB * $_COLORCONSTANTS_RGBMAX     Return $avArray EndFunc ; #INTERNAL_USE_ONLY# ============================================================================= ; Name...........: __ColorConvertHueToRGB ; Description ...: Helper function for converting HSL to RGB ; Syntax.........: __ColorConvertHueToRGB($nA, $nB, $nH) ; Parameters ....: $nA - Value A ;                  $nB - Value B ;                  $nH - Hue ; Return values .: A value based on value A and value B, dependent on the inputted hue ; Author ........: Ultima ; Modified.......: ; Remarks .......: For Internal Use Only ; Related .......: ; Link ..........; <a href='http://www.easyrgb.com/math.php?MATH=M19#text19' class='bbc_url' title='External link' rel='nofollow external'>http://www.easyrgb.com/math.php?MATH=M19#text19</a> ; Example .......; ; ================================================================================================= Func __ColorConvertHueToRGB($nA, $nB, $nH)     If $nH < 0 Then $nH += 1     If $nH > 1 Then $nH -= 1     If (6 * $nH) < 1 Then Return $nA + ($nB - $nA) * 6 * $nH     If (2 * $nH) < 1 Then Return $nB     If (3 * $nH) < 2 Then Return $nA + ($nB - $nA) * 6 * (2/3 - $nH)     Return $nA EndFunc

Test code:
Local $aiInput[3] = [128, 255, 128] $aiHSL = _ColorConvertRGBtoHSL($aiInput) $aiRGB = _ColorConvertHSLtoRGB($aiHSL) ConsoleWrite(StringFormat("| R: %.3f" & @TAB & "| H: %.3f" & @TAB & "| R: %.3f" & @CRLF, $aiInput[0], $aiHSL[0], $aiRGB[0])) ConsoleWrite(StringFormat("| G: %.3f" & @TAB & "| S: %.3f" & @TAB & "| G: %.3f" & @CRLF, $aiInput[1], $aiHSL[1], $aiRGB[1])) ConsoleWrite(StringFormat("| B: %.3f" & @TAB & "| L: %.3f" & @TAB & "| B: %.3f" & @CRLF, $aiInput[2], $aiHSL[2], $aiRGB[2])) ConsoleWrite(@CRLF) Local $aiInput[3] = [83, 220, 32] $aiRGB = _ColorConvertHSLtoRGB($aiInput) $aiHSL = _ColorConvertRGBtoHSL($aiRGB) ConsoleWrite(StringFormat("| H: %.3f" & @TAB & "| R: %.3f" & @TAB & "| H: %.3f" & @CRLF, $aiInput[0], $aiRGB[0], $aiHSL[0])) ConsoleWrite(StringFormat("| S: %.3f" & @TAB & "| G: %.3f" & @TAB & "| S: %.3f" & @CRLF, $aiInput[1], $aiRGB[1], $aiHSL[1])) ConsoleWrite(StringFormat("| L: %.3f" & @TAB & "| B: %.3f" & @TAB & "| L: %.3f" & @CRLF, $aiInput[2], $aiRGB[2], $aiHSL[2]))

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

#6 XxXFaNtA

XxXFaNtA

    Adventurer

  • Active Members
  • PipPip
  • 128 posts

Posted 07 February 2008 - 09:50 PM

nice nice nice :)
/

Posted Image


#7 James

James

    jbrooksuk

  • MVPs
  • 9,498 posts

Posted 12 February 2008 - 10:18 AM

Brilliant. People should add his to their Colour Mouse programs.

#8 AZJIO

AZJIO

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,079 posts

Posted 09 June 2011 - 07:04 PM

wikipedia - HSV (HSB)

HSB_RGB_BGR.au3

example GUI

Edited by AZJIO, 04 September 2012 - 02:10 PM.


#9 wakillon

wakillon

    Tiny Tools Coder

  • Active Members
  • PipPipPipPipPipPip
  • 2,495 posts

Posted 10 June 2011 - 01:59 PM

_RGB_BGR Function is missing...

  AutoIt Version : 3.3.8.1/3.3.9.4 SciTE 3.3.0 Language:040C OS:WIN_7/ CPU:X64 OS:X64 

  Last updated Scripts and executables with full embedded files are available on : GoogleCode 


#10 AZJIO

AZJIO

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,079 posts

Posted 10 June 2011 - 02:48 PM

Added
Used in CreationTheme and ColorText

Edited by AZJIO, 10 June 2011 - 03:10 PM.


#11 AZJIO

AZJIO

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,079 posts

Posted 10 June 2011 - 04:02 PM

See screenshot
-->-- _RGB_to_HSB String_Data ( 7895090 ) : H : 60, S : 58, B : 47

Edited by AZJIO, 10 June 2011 - 04:03 PM.


#12 AZJIO

AZJIO

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,079 posts

Posted 10 June 2011 - 04:33 PM

Name of forum topics "Convert RGB to HSB HSB based on Photoshop"
Photoshop: H[0-360], S[0-100], B[0-100]
Windows: H[0-240], S[0-240], B[0-240]

#13 wakillon

wakillon

    Tiny Tools Coder

  • Active Members
  • PipPipPipPipPipPip
  • 2,495 posts

Posted 10 June 2011 - 04:45 PM

Topic title didn't implied that the results would be completly differents than other RGB2HSL functions...

Anyway thanks for this precisions ! Posted Image

  AutoIt Version : 3.3.8.1/3.3.9.4 SciTE 3.3.0 Language:040C OS:WIN_7/ CPU:X64 OS:X64 

  Last updated Scripts and executables with full embedded files are available on : GoogleCode 


#14 AZJIO

AZJIO

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,079 posts

Posted 11 June 2012 - 12:20 AM

HSB <> HSL

Plain Text         
#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Color.au3> ; En $LngTitle = 'Color' $LngHue = 'Hue' $LngSaturation = 'Saturation' $LngLightness = 'Lightness' $LngRed = 'Red' $LngGreen = 'Green' $LngBlue = 'Blue' ; Ru ; если русская локализация, то русский язык If @OSLang = 0419 Then $LngTitle = 'Цвет' $LngHue = 'Тон' $LngSaturation = 'Насыщенность' $LngLightness = 'Светлота' $LngRed = 'Красный' $LngGreen = 'Зелёный' $LngBlue = 'Синий' EndIf Global $HSB[3] = [40, 240, 185], $RGB[3] $GUI = GUICreate($LngTitle, 340, 270) $iColorLabel = GUICtrlCreateLabel('', 90, 5, 200, 30, $WS_BORDER) $iNumLabel = GUICtrlCreateLabel('', 20, 12, 70, 17) GUICtrlCreateGroup('HSL', 3, 45, 333, 103) GUICtrlCreateGroup('RGB', 3, 155, 333, 103) GUICtrlCreateLabel($LngHue, 10, 61, 80, 17) $iValSld1 = GUICtrlCreateLabel($HSB[0], 300, 60, 30, 17) $slider1 = GUICtrlCreateSlider(90, 55, 200, 30) GUICtrlSetLimit(-1, 240, 0) GUICtrlSetData(-1, $HSB[0]) $hSlider_Handle1 = GUICtrlGetHandle(-1) GUICtrlCreateLabel($LngSaturation, 10, 91, 80, 17) $iValSld2 = GUICtrlCreateLabel($HSB[1], 300, 90, 30, 17) $slider2 = GUICtrlCreateSlider(90, 85, 200, 30) GUICtrlSetLimit(-1, 240, 0) GUICtrlSetData(-1, $HSB[1]) $hSlider_Handle2 = GUICtrlGetHandle(-1) GUICtrlCreateLabel($LngLightness, 10, 121, 80, 17) $iValSld3 = GUICtrlCreateLabel($HSB[2], 300, 120, 30, 17) $slider3 = GUICtrlCreateSlider(90, 115, 200, 30) GUICtrlSetLimit(-1, 240, 0) GUICtrlSetData(-1, $HSB[2]) $hSlider_Handle3 = GUICtrlGetHandle(-1) GUICtrlCreateLabel($LngRed, 10, 171, 80, 17) $iValSldRGB1 = GUICtrlCreateLabel($HSB[0], 300, 170, 30, 17) $sliderRGB1 = GUICtrlCreateSlider(90, 165, 200, 30) GUICtrlSetLimit(-1, 255, 0) GUICtrlSetData(-1, $HSB[0]) $hSlider_HandleRGB1 = GUICtrlGetHandle(-1) GUICtrlCreateLabel($LngGreen, 10, 201, 80, 17) $iValSldRGB2 = GUICtrlCreateLabel($HSB[1], 300, 200, 30, 17) $sliderRGB2 = GUICtrlCreateSlider(90, 195, 200, 30) GUICtrlSetLimit(-1, 255, 0) GUICtrlSetData(-1, $HSB[1]) $hSlider_HandleRGB2 = GUICtrlGetHandle(-1) GUICtrlCreateLabel($LngBlue, 10, 231, 80, 17) $iValSldRGB3 = GUICtrlCreateLabel($HSB[2], 300, 230, 30, 17) $sliderRGB3 = GUICtrlCreateSlider(90, 225, 200, 30) GUICtrlSetLimit(-1, 255, 0) GUICtrlSetData(-1, $HSB[2]) $hSlider_HandleRGB3 = GUICtrlGetHandle(-1) _SetColorRGB() GUISetState() GUIRegisterMsg($WM_HSCROLL, "WM_HSCROLL") Do Until GUIGetMsg() = -3 Func WM_HSCROLL($hWnd, $Msg, $wParam, $lParam) #forceref $Msg, $wParam, $lParam Local $nScrollCode = BitAND($wParam, 0xFFFF) ; _WinAPI_LoWord Local $value = BitShift($wParam, 16) ; _WinAPI_HiWord If $nScrollCode = 5 Then Switch $lParam Case $hSlider_Handle1 GUICtrlSetData($iValSld1, $value) $HSB[0] = $value _SetColorRGB() Case $hSlider_Handle2 GUICtrlSetData($iValSld2, $value) $HSB[1] = $value _SetColorRGB() Case $hSlider_Handle3 GUICtrlSetData($iValSld3, $value) $HSB[2] = $value _SetColorRGB() Case $hSlider_HandleRGB1 GUICtrlSetData($iValSldRGB1, $value) $RGB[0] = $value _SetColorHSB() Case $hSlider_HandleRGB2 GUICtrlSetData($iValSldRGB2, $value) $RGB[1] = $value _SetColorHSB() Case $hSlider_HandleRGB3 GUICtrlSetData($iValSldRGB3, $value) $RGB[2] = $value _SetColorHSB() EndSwitch EndIf Return $GUI_RUNDEFMSG EndFunc Func _SetColorRGB() $a = _ColorConvertHSLtoRGB($HSB) For $i = 0 To 2 $RGB[$i] = Round($a[$i]) Next $a = Hex($RGB[0], 2) & Hex($RGB[1], 2) & Hex($RGB[2], 2) GUICtrlSetData($iNumLabel, $a) GUICtrlSetBkColor($iColorLabel, Dec($a)) GUICtrlSetData($iValSldRGB1, $RGB[0]) GUICtrlSetData($iValSldRGB2, $RGB[1]) GUICtrlSetData($iValSldRGB3, $RGB[2]) GUICtrlSetData($sliderRGB1, $RGB[0]) GUICtrlSetData($sliderRGB2, $RGB[1]) GUICtrlSetData($sliderRGB3, $RGB[2]) EndFunc Func _SetColorHSB() $a = _ColorConvertRGBtoHSL($RGB) For $i = 0 To 2 $HSB[$i] = Round($a[$i]) Next $a = Hex($RGB[0], 2) & Hex($RGB[1], 2) & Hex($RGB[2], 2) GUICtrlSetData($iNumLabel, $a) GUICtrlSetBkColor($iColorLabel, Dec($a)) GUICtrlSetData($iValSld1, $HSB[0]) GUICtrlSetData($iValSld2, $HSB[1]) GUICtrlSetData($iValSld3, $HSB[2]) GUICtrlSetData($slider1, $HSB[0]) GUICtrlSetData($slider2, $HSB[1]) GUICtrlSetData($slider3, $HSB[2]) EndFunc

Photoshop uses the HSL (Ctrl + U)

Color.au3 UDF
H[0-240], S[0-240], L[0-240]
Global Const $__COLORCONSTANTS_HSLMAX = 240 ;... Local $nH = Number($avArray[0]) / $__COLORCONSTANTS_HSLMAX Local $nS = Number($avArray[1]) / $__COLORCONSTANTS_HSLMAX Local $nL = Number($avArray[2]) / $__COLORCONSTANTS_HSLMAX


Edit Color.au3
H[0-360], S[0-100], L[0-100]
Local $nH = Number($avArray[0]) / 360 Local $nS = Number($avArray[1]) / 100 Local $nL = Number($avArray[2]) / 100

Edited by AZJIO, 16 June 2012 - 11:08 PM.


#15 AZJIO

AZJIO

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,079 posts

Posted 04 September 2012 - 02:12 PM

Update HSB_RGB_BGR.au3 ^^^




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users