Beispiel #1
0
 def validate_and_return_luma_value(self, b, g, r, a=255):
     col = Color(r, g, b, a)
     luma_value = int(round(0.299 * r + 0.587 * g + 0.114 * b))
     self.failUnlessEqual(luma_value, col.mono())
     self.failIf(luma_value < 0)
     self.failIf(luma_value > 255)
     return luma_value
Beispiel #2
0
 def test_random_generation_of_colour(self, mock_randint):
     expected = Color(100, 100, 100, 255)
     generated = Color.random()
     mock_randint.assert_has_calls([
         call(0, 255),
         call(0, 255),
         call(0, 255)
     ])
     self.failUnlessEqual(expected, generated)
Beispiel #3
0
 def test_open_cv_bgra_format(self):
     col = Color(46, 0, 255, 123)
     b, g, r, a = col.bgra()
     self.failUnlessEqual(b, 255)
     self.failUnlessEqual(g, 0)
     self.failUnlessEqual(r, 46)
     self.failUnlessEqual(a, 123)
     b, g, r = col.bgr()
     self.failUnlessEqual(b, 255)
     self.failUnlessEqual(g, 0)
     self.failUnlessEqual(r, 46)
Beispiel #4
0
    def draw_polygon(self, points, color=Color.black(), thickness=2):
        """ Draw a polygon with vertices given by points. """
        i = 0
        while i < len(points) - 1:
            self.draw_line(points[i], points[i + 1], color, thickness)
            i += 1

        self.draw_line(points[i], points[0], color, thickness)
Beispiel #5
0
    def draw_text(self, text, point, color=Color.black(), centered=False, scale=1.5, thickness=3):
        """ Draw the specified text on the image (in place) """
        position = point
        if centered:
            size = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, fontScale=scale, thickness=thickness)[0]
            position = point + Point(-size[0]/2, +size[1]/2)

        position = position.intify().tuple()
        cv2.putText(self._img, text, position, cv2.FONT_HERSHEY_SIMPLEX, scale, color.bgra(), thickness)
Beispiel #6
0
 def test_true_string_representation_has_correct_format(self):
     col = Color(46, 0, 255, 123)
     self.failUnlessEqual("Color(46, 0, 255, 123)", repr(col))
Beispiel #7
0
 def test_to_string_has_correct_format(self):
     col = Color(46, 0, 255, 123)
     self.failUnlessEqual("46,0,255,123", str(col))
Beispiel #8
0
 def draw_dot(self, point, color=Color.black(), thickness=5):
     """ Draw the specified dot on the image (in place) """
     cv2.circle(self._img, point.intify().tuple(), 0, color.bgra(), thickness)
Beispiel #9
0
 def test_colour_from_string_with_explicit_separator(self):
     self.failUnlessEqual(Color(123, 234, 45, 255), Color.from_string("123; 234;45;  255", sep=";"))
Beispiel #10
0
 def test_colour_from_string_with_rgb_values_plus_alpha(self):
     self.failUnlessEqual(Color(123, 234, 45, 255), Color.from_string("123,234,45,255"))
Beispiel #11
0
 def blank(width, height, channels=3, color=Color.black()):
     """ Return a new empty image of the specified size. """
     blank = Image(np.full((height, width, channels), 0, np.uint8))
     blank.draw_rectangle(blank.bounds(), color, thickness=-1)
     return blank
Beispiel #12
0
 def draw_cross(self, point, color=Color.black(), size=5, thickness=1):
     """ Draw an X on the image (in place). """
     self.draw_line(point - Point(size, size), point + Point(size, size), color, thickness)
     self.draw_line(point + Point(size, -size), point + Point(-size, size), color, thickness)
Beispiel #13
0
 def draw_line(self, pt1, pt2, color=Color.black(), thickness=2):
     """ Draw the specified line on the image (in place) """
     cv2.line(self._img, pt1.intify().tuple(), pt2.intify().tuple(), color.bgra(), thickness=thickness)
Beispiel #14
0
 def draw_rectangle(self, rect, color=Color.black(), thickness=1):
     """ Draw the specified rectangle on the image (in place) """
     tl, br = rect.intify().top_left().tuple(), rect.intify().bottom_right().tuple()
     cv2.rectangle(self._img, tl, br, color.bgra(), thickness=thickness)
Beispiel #15
0
 def draw_circle(self, point, radius, color=Color.black(), thickness=2):
     """ Draw the specified circle on the image (in place) """
     cv2.circle(self._img, point.intify().tuple(), int(radius), color.bgra(), thickness)
Beispiel #16
0
 def test_hex_conversion(self):
     self.failUnlessEqual("#4286f4", Color(66, 134, 244).to_hex())
     self.failUnlessEqual("#071223", Color(7, 18, 35).to_hex())
     self.failUnlessEqual("#8e2525", Color(142, 37, 37).to_hex())
     self.failUnlessEqual("#1c8415", Color(28, 132, 21).to_hex())
Beispiel #17
0
 def test_colour_from_string_with_rgb_values(self):
     self.failUnlessEqual(Color(123, 234, 45), Color.from_string("123,234,45"))
Beispiel #18
0
 def test_equality_operator_fail_with_invalid_type(self):
     null_var = None
     color = Color(1, 2, 3, 4)
     self.failUnlessRaises(TypeError, color.__eq__, null_var)
     self.failUnlessRaises(TypeError, color.__eq__, "Not a Color")
     self.failUnlessRaises(TypeError, color.__eq__, 4.6)
Beispiel #19
0
 def test_colour_from_string_is_whitespace_indifferent(self):
     self.failUnlessEqual(Color(123, 234, 45), Color.from_string("123, 234,    45"))
     self.failUnlessEqual(Color(123, 234, 45, 255), Color.from_string("123,  234,    45,  255"))
Beispiel #20
0
 def test_default_alpha_value_is_255(self):
     col = Color(0, 56.7, 255)
     self.failUnlessEqual(0, col.r)
     self.failUnlessEqual(56, col.g)
     self.failUnlessEqual(255, col.b)
     self.failUnlessEqual(255, col.a)
Beispiel #21
0
 def test_equality_operator(self):
     self.failUnless(Color(1, 2, 3, 4) == Color(1, 2, 3, 4))
     self.failIf(Color(1, 2, 3, 4) == Color(0, 2, 3, 4))
     self.failIf(Color(1, 2, 3, 4) == Color(1, 0, 3, 4))
     self.failIf(Color(1, 2, 3, 4) == Color(1, 2, 0, 4))
     self.failIf(Color(1, 2, 3, 4) == Color(1, 2, 3, 0))
Beispiel #22
0
 def from_file_string(self, string):
     self._value = Color.from_string(string)