Skip to content

Commit

Permalink
ColorChooser : Optimize slider redraws
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmehl committed Nov 13, 2024
1 parent a3d6614 commit ec79d5b
Showing 1 changed file with 45 additions and 33 deletions.
78 changes: 45 additions & 33 deletions python/GafferUI/ColorChooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,18 @@ def __init__( self, color, component, dynamicBackground = True, **kw ) :
self.color = color
self.component = component
self.__dynamicBackground = dynamicBackground
self.__gradientToDraw = None
self.__size = self.size()

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

if (
( self.__dynamicBackground and color != self.color ) or
( not self.__dynamicBackground and self.component == "s" )
) :
self.__gradientToDraw = None
self.color = color
self._qtWidget().update()

Expand All @@ -144,6 +151,7 @@ def getColor( self ) :
def setDynamicBackground( self, dynamicBackground ) :

self.__dynamicBackground = dynamicBackground
self.__gradientToDraw = None
self._qtWidget().update()

def getDynamicBackground( self ) :
Expand All @@ -153,43 +161,46 @@ def getDynamicBackground( self ) :
def _drawBackground( self, painter ) :

size = self.size()
grad = QtGui.QLinearGradient( 0, 0, size.x, 0 )

displayTransform = self.displayTransform()
if self.__gradientToDraw is None or size != self.__size :
self.__gradientToDraw = QtGui.QLinearGradient( 0, 0, size.x, 0 )

if self.component == "a" :
c1 = imath.Color3f( 0 )
c2 = imath.Color3f( 1 )
else :
if self.__dynamicBackground :
c1 = imath.Color3f( self.color[0], self.color[1], self.color[2] )
elif self.component in "rgbvi" :
displayTransform = self.displayTransform()

if self.component == "a" :
c1 = imath.Color3f( 0 )
elif self.component == "h" :
c1 = imath.Color3f( 0, 1, 1 )
elif self.component == "s" :
c1 = imath.Color3f( self.color[0], 0, 1 )
elif self.component in "tm" :
c1 = imath.Color3f( 0, 0, 0.5 )
c2 = imath.Color3f( c1 )
a = { "r" : 0, "g" : 1, "b" : 2, "h" : 0, "s" : 1, "v": 2, "t" : 0, "m" : 1, "i" : 2 }[self.component]
c1[a] = -1 if self.component in "tm" else 0
c2[a] = 1

numStops = max( 2, size.x // 2 )
for i in range( 0, numStops ) :

t = float( i ) / (numStops-1)
c = c1 + (c2-c1) * t
if self.component in "hsv" :
c = c.hsv2rgb()
elif self.component in "tmi" :
c = _tmiToRGB( c )

grad.setColorAt( t, self._qtColor( displayTransform( c ) ) )

brush = QtGui.QBrush( grad )
c2 = imath.Color3f( 1 )
else :
if self.__dynamicBackground :
c1 = imath.Color3f( self.color[0], self.color[1], self.color[2] )
elif self.component in "rgbvi" :
c1 = imath.Color3f( 0 )
elif self.component == "h" :
c1 = imath.Color3f( 0, 1, 1 )
elif self.component == "s" :
c1 = imath.Color3f( self.color[0], 0, 1 )
elif self.component in "tm" :
c1 = imath.Color3f( 0, 0, 0.5 )
c2 = imath.Color3f( c1 )
a = { "r" : 0, "g" : 1, "b" : 2, "h" : 0, "s" : 1, "v": 2, "t" : 0, "m" : 1, "i" : 2 }[self.component]
c1[a] = -1 if self.component in "tm" else 0
c2[a] = 1

numStops = max( 2, size.x // 2 )
for i in range( 0, numStops ) :

t = float( i ) / (numStops-1)
c = c1 + (c2-c1) * t
if self.component in "hsv" :
c = c.hsv2rgb()
elif self.component in "tmi" :
c = _tmiToRGB( c )

self.__gradientToDraw.setColorAt( t, self._qtColor( displayTransform( c ) ) )

brush = QtGui.QBrush( self.__gradientToDraw )
painter.fillRect( 0, 0, size.x, size.y, brush )
self.__size = size

def _drawValue( self, painter, value, position, state ) :

Expand Down Expand Up @@ -228,6 +239,7 @@ def _drawValue( self, painter, value, position, state ) :
def _displayTransformChanged( self ) :

GafferUI.Slider._displayTransformChanged( self )
self.__gradientToDraw = None
self._qtWidget().update()

class _ColorField( GafferUI.Widget ) :
Expand Down

0 comments on commit ec79d5b

Please sign in to comment.