Skip to content

Commit

Permalink
fixup! ColorChooser : Add _ColorField widget
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmehl committed Aug 2, 2024
1 parent d974306 commit 2bddfe6
Showing 1 changed file with 36 additions and 20 deletions.
56 changes: 36 additions & 20 deletions python/GafferUI/ColorChooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,44 +387,60 @@ def __drawValue( self, painter ) :
painter.setBrush( QtGui.QBrush( color ) )

size = self.size()

# Use a dot when both axes are a valid value.
if position.x >= 0 and position.y >= 0 and position.x <= size.x and position.y <= size.y :
painter.drawEllipse( QtCore.QPoint( position.x, position.y ), 4.5, 4.5 )
return

# Draw a triangle pointing out of the field. Prioritize corners to avoid cutting
# off part of the triangle out of the field when one axis is right at 0 or size.
# |\ <-- We don't want this overhang.
# -------|->
# |/|
# |
# |
if position.x >= size.x and position.y <= 0 :
triangleWidth = 5.0
triangleSpacing = 2.0
positionClamped = imath.V2f(
min( max( 0.0, position.x ), size.x ),
min( max( 0.0, position.y ), size.y )
)
offset = imath.V2f( 0 )
# Use a corner triangle if both axes are invalid values.
if position.x > size.x and position.y < 0 :
rotation = -45.0 # Triangle pointing to the top-right
elif position.x <= 0 and position.y <= 0 :
offset = imath.V2f( -triangleSpacing, triangleSpacing )
elif position.x < 0 and position.y < 0 :
rotation = -135.0 # Top-left
elif position.x <= 0 and position.y >= size.y :
offset = imath.V2f( triangleSpacing, triangleSpacing )
elif position.x < 0 and position.y > size.y :
rotation = -225.0 # Bottom-left
elif position.x >= size.x and position.y >= size.y :
offset = imath.V2f( triangleSpacing, -triangleSpacing )
elif position.x > size.x and position.y > size.y :
rotation = -315.0 # Bottom-right
offset = imath.V2f( -triangleSpacing, -triangleSpacing )

elif position.x <= size.x and position.x >= 0 and position.y <= 0 :
# Finally, use a top / left / bottom / right triangle if one axis is an invalid value.
elif position.y < 0 :
rotation = -90.0 # Top
elif position.x <= 0 and position.y >= 0 and position.y <= size.y :
offset = imath.V2f( 0, triangleSpacing )
# Clamp it in more to account for the triangle size
positionClamped.x = min( max( triangleWidth + triangleSpacing, positionClamped.x ), size.x - triangleWidth - triangleSpacing )
elif position.x < 0 :
rotation = -180.0 # Left
elif position.x >= 0 and position.x <= size.x and position.y >= size.y :
offset = imath.V2f( triangleSpacing, 0 )
positionClamped.y = min( max( triangleWidth + triangleSpacing, positionClamped.y ), size.y - triangleWidth - triangleSpacing )
elif position.y > size.y :
rotation = -270.0 # Bottom
offset = imath.V2f( 0, -triangleSpacing )
positionClamped.x = min( max( triangleWidth + triangleSpacing, positionClamped.x ), size.x - triangleWidth - triangleSpacing )
else :
rotation = 0.0 # Right
offset = imath.V2f( -triangleSpacing, 0 )
positionClamped.y = min( max( triangleWidth + triangleSpacing, positionClamped.y ), size.y - triangleWidth - triangleSpacing )

rightPoints = [ imath.V2f( 0, 0 ), imath.V2f( -6, 5 ), imath.V2f( -6, -5 ) ]
rightPoints = [ imath.V2f( 0, 0 ), imath.V2f( -6, triangleWidth ), imath.V2f( -6, -triangleWidth ) ]
xform = imath.M33f().rotate( math.radians( rotation ) ) * imath.M33f().translate(
imath.V2f(
min( max( 0.0, position.x ), size.x ),
min( max( 0.0, position.y ), size.y )
)
positionClamped + offset
)
points = [ p * xform for p in rightPoints ]
points = [ QtCore.QPoint( p.x, p.y ) for p in points ]
# Transforming the points introduces slight precision errors which can be noticeable
# when drawing polygons. Round the values to compensate.
points = [ QtCore.QPoint( round( p.x ), round( p.y ) ) for p in points ]

painter.drawConvexPolygon( points )

Expand Down

0 comments on commit 2bddfe6

Please sign in to comment.