Skip to content

Commit

Permalink
ColorChooser : Maintain hue, saturation at zero
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmehl authored and johnhaddon committed Jun 24, 2024
1 parent 54af42f commit 5b1070c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
4 changes: 3 additions & 1 deletion Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
Improvements
------------

- ColorChooser : Added channel names to identify sliders.
- ColorChooser :
- Added channel names to identify sliders.
- Setting the saturation to zero no longer resets the hue and setting the value to zero no longer resets the hue and saturation.
- RenderPassEditor : Added "Select Affected Objects" popup menu item.
- Annotations :
- Added support for `{plug}` value substitutions in node annotations.
Expand Down
46 changes: 31 additions & 15 deletions python/GafferUI/ColorChooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def __init__( self, color, component, **kw ) :
self.color = color
self.component = component

# Sets the slider color in RGB space for RGBA channels and
# HSV space for HSV channels.
def setColor( self, color ) :

self.color = color
Expand All @@ -82,9 +84,6 @@ def _drawBackground( self, painter ) :
else :
c1 = imath.Color3f( self.color[0], self.color[1], self.color[2] )
c2 = imath.Color3f( self.color[0], self.color[1], self.color[2] )
if self.component in "hsv" :
c1 = c1.rgb2hsv()
c2 = c2.rgb2hsv()
a = { "r" : 0, "g" : 1, "b" : 2, "h" : 0, "s" : 1, "v": 2 }[self.component]
c1[a] = 0
c2[a] = 1
Expand Down Expand Up @@ -118,6 +117,7 @@ def __init__( self, color=imath.Color3f( 1 ), **kw ) :
GafferUI.Widget.__init__( self, self.__column, **kw )

self.__color = color
self.__colorHSV = self.__color.rgb2hsv()
self.__defaultColor = color

self.__sliders = {}
Expand Down Expand Up @@ -233,33 +233,47 @@ def __componentValueChanged( self, componentWidget, reason ) :
if componentWidget.component in ( "a", "h", "s" ) :
componentValue = min( componentValue, 1 )

newColor = self.__color.__class__( self.__color )
if componentWidget.component in ( "r", "g", "b", "a" ) :
newColor = self.__color.__class__( self.__color )

a = { "r" : 0, "g" : 1, "b" : 2, "a" : 3 }[componentWidget.component]
newColor[a] = componentValue

self.__setColorInternal( newColor, reason )
else :
newColor = newColor.rgb2hsv()
newColor = self.__colorHSV.__class__( self.__colorHSV )

a = { "h" : 0, "s" : 1, "v" : 2 }[componentWidget.component]
newColor[a] = componentValue
newColor = newColor.hsv2rgb()

self.__setColorInternal( newColor, reason )
self.__setColorInternal( newColor, reason, True )

def __setColorInternal( self, color, reason ) :
def __setColorInternal( self, color, reason, hsv = False ) :

dragBeginOrEnd = reason in (
GafferUI.Slider.ValueChangedReason.DragBegin,
GafferUI.Slider.ValueChangedReason.DragEnd,
GafferUI.NumericWidget.ValueChangedReason.DragBegin,
GafferUI.NumericWidget.ValueChangedReason.DragEnd,
)
if color != self.__color or dragBeginOrEnd :

previousColor = self.__colorHSV if hsv else self.__color

if color != previousColor or dragBeginOrEnd :
# we never optimise away drag begin or end, because it's important
# that they emit in pairs.
self.__color = color
self.__colorSwatch.setColor( color )
colorRGB = color.hsv2rgb() if hsv else color
self.__color = colorRGB
self.__colorSwatch.setColor( colorRGB )
self.__colorChangedSignal( self, reason )

hsv = color if hsv else color.rgb2hsv()

hsv[0] = hsv[0] if hsv[1] > 1e-7 and hsv[2] > 1e-7 else self.__colorHSV[0]
hsv[1] = hsv[1] if hsv[2] > 1e-7 else self.__colorHSV[1]

self.__colorHSV = hsv

## \todo This is outside the conditional because the clamping we do
# in __componentValueChanged means the color value may not correspond
# to the value in the ui, even if it hasn't actually changed. Move this
Expand All @@ -273,7 +287,7 @@ def __updateUIFromColor( self ) :

c = self.getColor()

for slider in self.__sliders.values() :
for slider in [ v for k, v in self.__sliders.items() if k in "rgba" ] :
slider.setColor( c )

for component, index in ( ( "r", 0 ), ( "g", 1 ), ( "b", 2 ) ) :
Expand All @@ -287,7 +301,9 @@ def __updateUIFromColor( self ) :
else :
self.__sliders["a"].parent().setVisible( False )

c = c.rgb2hsv()
for slider in [ v for k, v in self.__sliders.items() if k in "hsv" ] :
slider.setColor( self.__colorHSV )

for component, index in ( ( "h", 0 ), ( "s", 1 ), ( "v", 2 ) ) :
self.__sliders[component].setValue( c[index] )
self.__numericWidgets[component].setValue( c[index] )
self.__sliders[component].setValue( self.__colorHSV[index] )
self.__numericWidgets[component].setValue( self.__colorHSV[index] )

0 comments on commit 5b1070c

Please sign in to comment.