Jump to content

More _Color UDFs


InunoTaishou
 Share

Recommended Posts

After spending 15 minutes last night trying to get the RGB from an ARGB color I had remembered BitXOR. After figuring that one out I set forth a clean way to increase and decrease an individual color component in a color (without having to do something like Hex(_ColorGetRed(0xFF0000) - 10, 2). I don't really know a lot about Bit operations so it was interesting to see how it works and finally figured it out when I was looking closely at how the Color UDF got the Red, green, and blue components from a color.

; #Functions# ===================================================================================================================
; _ColorRGB             - Return the RGB color of the individual components.
; _ColorARGB            - Return the ARGB color of the individual components.
; _ColorGetAlpha        - Returns the alpha value of the color. 0 - 255
; _ColorAdjustAlpha     - Adjust (increase or decrease) the alpha component of a color.
; _ColorAdjustRed       - Adjust (increase or decrease) the red component of a color.
; _ColorAdjustGreen     - Adjust (increase or decrease) the green component of a color.
; _ColorAdjustBlue      - Adjust (increase or decrease) the blue component of a color.
; ===============================================================================================================================

Func _ColorRGB($iR, $iG, $iB, Const $_iCurrentExtended = @extended)
    Return SetExtended($_iCurrentExtended, BitOR(BitShift($iR, -16), BitShift($iG, -8), $iB))
EndFunc   ;==>_ColorRGB

Func _ColorARGB($iA, $iR, $iG, $iB, Const $_iCurrentExtended = @extended)
    Return SetExtended($_iCurrentExtended, BitOR(BitShift($iA, -24), BitShift($iR, -16), BitShift($iG, -8), $iB))
EndFunc   ;==>_ColorARGB

Func _ColorGetAlpha($iColor, Const $_iCurrentExtended = @extended)
    If (Not BitAND($iColor, 0xFF000000)) Then Return SetError(1, 0, 0)
    Return SetExtended($_iCurrentExtended, BitAND(BitShift($iColor, 24), 0xFF))
EndFunc   ;==>_ColorGetAlpha

Func _ColorAdjustAlpha($iColor, $iAmount, Const $_iCurrentExtended = @extended)
    If (Not BitAND($iColor, 0xFF000000) And $iAmount < 0) Then Return SetError(1, 0, $iColor)
    Local $iA = BitAND(BitShift($iColor, 24), 0xFF)
    If ($iAmount < 0 And $iA + $iAmount < 0) Then $iAmount = -$iA
    If ($iAmount > 0 And $iA = 255) Then Return $iColor
    If ($iAmount > 0 And $iA + $iAmount > 255) Then $iAmount = Abs($iA - $iAmount)
    Return SetExtended($_iCurrentExtended, $iColor + BitShift($iAmount, -24))
EndFunc   ;==>_ColorAdjustAlpha

Func _ColorAdjustRed($iColor, $iAmount, Const $_iCurrentExtended = @extended)
    If (Not BitAND($iColor, 0xFF0000) And $iAmount < 0) Then Return SetError(1, 0, $iColor)
    Local $iR = BitAND(BitShift($iColor, 16), 0xFF)
    If ($iAmount < 0 And $iR + $iAmount < 0) Then $iAmount = -$iR
    If ($iAmount > 0 And $iR = 255) Then Return $iColor
    If ($iAmount > 0 And $iR + $iAmount > 255) Then $iAmount = Abs($iR - $iAmount)
    Return SetExtended($_iCurrentExtended, $iColor + BitShift($iAmount, -16))
EndFunc   ;==>_ColorAdjustRed

Func _ColorAdjustGreen($iColor, $iAmount, Const $_iCurrentExtended = @extended)
    If (Not BitAND($iColor, 0xFF00) And $iAmount < 0) Then Return SetError(1, 0, $iColor)
    Local $iG = BitAND(BitShift($iColor, 8), 0xFF)
    If ($iAmount < 0 And $iG + $iAmount < 0) Then $iAmount = -$iG
    If ($iAmount > 0 And $iG = 255) Then Return $iColor
    If ($iAmount > 0 And $iG + $iAmount > 255) Then $iAmount = Abs($iG - $iAmount)
    Return SetExtended($_iCurrentExtended, $iColor + BitShift($iAmount, -8))
EndFunc   ;==>_ColorAdjustGreen

Func _ColorAdjustBlue($iColor, $iAmount, Const $_iCurrentExtended = @extended)
    If (Not BitAND($iColor, 0xFF) And $iAmount < 0) Then Return SetError(1, 0, $iColor)
    Local $iB = BitAND($iColor, 0xFF)
    If ($iAmount < 0 And $iB + $iAmount < 0) Then $iAmount = -$iB
    If ($iAmount > 0 And $iB = 255) Then Return $iColor
    If ($iAmount > 0 And $iB + $iAmount > 255) Then $iAmount = Abs($iB - $iAmount)
    Return SetExtended($_iCurrentExtended, $iColor + $iAmount)
EndFunc   ;==>_ColorAdjustBlue

A quick example using them.

Global $iBaseRGB = 0x00FFFF
Global $iBaseARGB = 0xC8000000
Global $iBaseAlpha = _ColorGetAlpha($iBaseARGB)
Global $iRemoveG = _ColorAdjustGreen($iBaseRGB, -255)
Global $iRemoveGB = _ColorAdjustBlue($iRemoveG, -255)
Global $iRemoveGBAddR = _ColorAdjustRed($iRemoveGB, 255)
Global $iRemoveGBAddRA = _ColorAdjustAlpha($iRemoveGBAddR, $iBaseAlpha)

MsgBox("", "Adjusting colors", "Base RGB color: 0x" & Hex($iBaseRGB, 6) & @CRLF & _
        "Remove Green component: 0x" & Hex($iRemoveG, 6) & @CRLF & _
        "Remove Blue component: 0x" & Hex($iRemoveGB, 6) & @CRLF & _
        "Add Red component: 0x" & Hex($iRemoveGBAddR, 6) & @CRLF & @CRLF & _
        "Base ARGB color: 0x" & Hex($iBaseARGB, 8) & @CRLF & _
        "Alpha for $iBaseAlpha: 0x" & Hex($iBaseAlpha, 2) & @CRLF & _
        "Add $iBaseAlpha to $iRemoveGBAddR: 0x" & Hex($iRemoveGBAddRA, 8) & @CRLF & _
        "Max Alpha for $iRemoveGBAddRA: 0x" & Hex(_ColorAdjustAlpha($iRemoveGBAddRA, 255)) & @CRLF & @CRLF & _
        "RGB for Red 31, Green 31, Blue 31: 0x" & Hex(_ColorRGB(31, 31, 31), 6) & @CRLF & _
        "ARGB for Alpha 255, Red 42, Green 42, and Blue 42: 0x" & Hex(_ColorARGB(255, 42, 42, 42), 8))

All my bit operations and math should be right, I tested each function. No need to calculate how much you need to add, or subtract, to a component to max, or 0, it. Just use the max value (255 or -255).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...