From 031cab0b3984ba1b87ce1f94797689e5fd286de7 Mon Sep 17 00:00:00 2001 From: Jayakumar Chinnappan Date: Sat, 18 May 2013 22:33:11 +0530 Subject: [PATCH] Adding string representations to Color --- py/selenium/webdriver/support/color.py | 11 ++++++++--- py/test/selenium/webdriver/support/color_tests.py | 5 +++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/py/selenium/webdriver/support/color.py b/py/selenium/webdriver/support/color.py index dc8b94bb93858..d21ecbe4102e8 100644 --- a/py/selenium/webdriver/support/color.py +++ b/py/selenium/webdriver/support/color.py @@ -119,7 +119,7 @@ def __init__(self, red, green, blue, alpha=1): self.red = int(red) self.green = int(green) self.blue = int(blue) - self.alpha = float(alpha) or 0 + self.alpha = "1" if float(alpha) == 1 else str(float(alpha) or 0) @property def rgb(self): @@ -127,8 +127,7 @@ def rgb(self): @property def rgba(self): - a = "1" if self.alpha == 1 else str(self.alpha) - return "rgba(%d, %d, %d, %s)" % (self.red, self.green, self.blue, a) + return "rgba(%d, %d, %d, %s)" % (self.red, self.green, self.blue, self.alpha) @property def hex(self): @@ -148,6 +147,12 @@ def __ne__(self, other): def __hash__(self): return hash((self.red, self.green, self.blue, self.alpha)) + def __repr__(self): + return "Color(red=%d, green=%d, blue=%d, alpha=%s)" % (self.red, self.green, self.blue, self.alpha) + + def __str__(self): + return "Color: %s" % self.rgba + # Basic, extended and transparent colour keywords as defined by the W3C HTML4 spec # See http://www.w3.org/TR/css3-color/#html4 diff --git a/py/test/selenium/webdriver/support/color_tests.py b/py/test/selenium/webdriver/support/color_tests.py index 580643e8d6afb..059f386acfa59 100644 --- a/py/test/selenium/webdriver/support/color_tests.py +++ b/py/test/selenium/webdriver/support/color_tests.py @@ -100,3 +100,8 @@ def test_hash(self): hash1 = hash(Color.from_string("#f00")) hash2 = hash(Color.from_string("rgb(255, 0, 0)")) assert hash1 == hash2 + + def test_string_representations(self): + hex_ = "#01Ff03" + assert str(Color.from_string(hex_)) == "Color: rgba(1, 255, 3, 1)" + assert repr(Color.from_string(hex_)) == "Color(red=1, green=255, blue=3, alpha=1)"