Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test setting colors, and fixes based on tests. #1052

Merged
merged 3 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions kiva/basecore2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,10 +828,11 @@ def set_fill_color(self, color):
between 0.0 and 1.0
"""
if len(color) == 3:
self.state.fill_color[:3] = color
self.state.fill_color[3] = 1.0
r, g, b = color
a = 1.0
else:
self.state.fill_color[:] = color
r, g, b, a = color
self.state.line_state.fill_color[:] = r, g, b, a

def get_fill_color(self, color):
"""
Expand All @@ -856,10 +857,11 @@ def set_stroke_color(self, color):
between 0.0 and 1.0
"""
if len(color) == 3:
self.state.line_state.line_color[:3] = color
self.state.line_state.line_color[3] = 1.0
r, g, b = color
a = 1.0
else:
self.state.line_state.line_color[:] = color
r, g, b, a = color
self.state.line_state.line_color[:] = r, g, b, a

def get_stroke_color(self, color):
"""
Expand Down
16 changes: 8 additions & 8 deletions kiva/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,21 +513,21 @@ def set_rendering_intent(self):
def set_fill_color(self, color):
"""
"""
r, g, b = color[:3]
try:
a = color[3]
except IndexError:
if len(color) == 3:
r, g, b = color
a = 1.0
else:
r, g, b, a = color
self.gc.setFillColorRGB(r, g, b, a)

def set_stroke_color(self, color):
"""
"""
r, g, b = color[:3]
try:
a = color[3]
except IndexError:
if len(color) == 3:
r, g, b = color
a = 1.0
else:
r, g, b, a = color
self.gc.setStrokeColorRGB(r, g, b, a)

def set_alpha(self, alpha):
Expand Down
16 changes: 8 additions & 8 deletions kiva/qpainter.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,23 +527,23 @@ def set_rendering_intent(self):
def set_fill_color(self, color):
"""
"""
r, g, b = color[:3]
try:
a = color[3]
except IndexError:
if len(color) == 3:
r, g, b = color
a = 1.0
else:
r, g, b, a = color
brush = self.gc.brush()
brush.setColor(QtGui.QColor.fromRgbF(r, g, b, a))
self.gc.setBrush(brush)

def set_stroke_color(self, color):
"""
"""
r, g, b = color[:3]
try:
a = color[3]
except IndexError:
if len(color) == 3:
r, g, b = color
a = 1.0
else:
r, g, b, a = color
pen = self.gc.pen()
pen.setColor(QtGui.QColor.fromRgbF(r, g, b, a))
self.gc.setPen(pen)
Expand Down
18 changes: 9 additions & 9 deletions kiva/quartz/ABCGI.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -673,23 +673,23 @@ cdef class CGContext:
def set_fill_color(self, object color):
"""
"""
r,g,b = color[:3]
try:
a = color[3]
except IndexError:
if len(color) == 3:
r, g, b = color
a = 1.0
elif len(color) == 4:
r, g, b, a = color
self.fill_color = (r,g,b,a)
CGContextSetRGBFillColor(self.context, r, g, b, a)

def set_stroke_color(self, object color):
"""
"""
r,g,b = color[:3]
try:
a = color[3]
except IndexError:
if len(color) == 3:
r, g, b = color
a = 1.0
self.stroke_color = (r,g,b,a)
elif len(color) == 4:
r, g, b, a = color
self.stroke_color = (r, g, b, a)
CGContextSetRGBStrokeColor(self.context, r, g, b, a)

def set_alpha(self, float alpha):
Expand Down
47 changes: 47 additions & 0 deletions kiva/tests/drawing_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@
]


rgba_float_dtype = numpy.dtype([
('red', "float64"),
('green', "float64"),
('blue', "float64"),
('alpha', "float64"),
])
rgb_float_dtype = numpy.dtype([
('red', "float64"),
('green', "float64"),
('blue', "float64"),
])


class DrawingTester(object):
""" Basic drawing tests for graphics contexts.
"""
Expand Down Expand Up @@ -205,6 +218,40 @@ def test_draw_path_at_points(self):
self.gc.draw_path_at_points(points, path, FILL_STROKE)
self.gc.fill_path()

def test_set_stroke_color(self):
# smoke tests for different color types that should be accepted
colors = [
(0.4, 0.2, 0.6),
[0.4, 0.2, 0.6],
numpy.array([0.4, 0.2, 0.6]),
numpy.array([(0.4, 0.2, 0.6)], dtype=rgb_float_dtype)[0],
(0.4, 0.2, 0.6, 1.0),
[0.4, 0.2, 0.6, 1.0],
numpy.array([0.4, 0.2, 0.6, 1.0]),
numpy.array([(0.4, 0.2, 0.6, 1.0)], dtype=rgba_float_dtype)[0],
]
for color in colors:
with self.subTest(color=color):
with self.gc:
self.gc.set_stroke_color(color)

def test_set_fill_color(self):
# smoke tests for different color types that should be accepted
colors = [
(0.4, 0.2, 0.6),
[0.4, 0.2, 0.6],
numpy.array([0.4, 0.2, 0.6]),
numpy.array([(0.4, 0.2, 0.6)], dtype=rgb_float_dtype)[0],
(0.4, 0.2, 0.6, 1.0),
[0.4, 0.2, 0.6, 1.0],
numpy.array([0.4, 0.2, 0.6, 1.0]),
numpy.array([(0.4, 0.2, 0.6, 1.0)], dtype=rgba_float_dtype)[0],
]
for color in colors:
with self.subTest(color=color):
with self.gc:
self.gc.set_fill_color(color)

# Required methods ####################################################

@contextlib.contextmanager
Expand Down