Skip to content
Closed
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
11 changes: 8 additions & 3 deletions py/selenium/webdriver/support/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,15 @@ 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):
return "rgb(%d, %d, %d)" % (self.red, self.green, self.blue)

@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):
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions py/test/selenium/webdriver/support/color_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"