From ec79d5b9a37ba8afa1ee3543819edd41399bf2a3 Mon Sep 17 00:00:00 2001 From: Eric Mehl Date: Thu, 24 Oct 2024 10:57:46 -0400 Subject: [PATCH] ColorChooser : Optimize slider redraws --- python/GafferUI/ColorChooser.py | 78 +++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/python/GafferUI/ColorChooser.py b/python/GafferUI/ColorChooser.py index 6d9c3eab4c6..d9f9f19d9bb 100644 --- a/python/GafferUI/ColorChooser.py +++ b/python/GafferUI/ColorChooser.py @@ -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() @@ -144,6 +151,7 @@ def getColor( self ) : def setDynamicBackground( self, dynamicBackground ) : self.__dynamicBackground = dynamicBackground + self.__gradientToDraw = None self._qtWidget().update() def getDynamicBackground( self ) : @@ -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 ) : @@ -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 ) :