1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import collections
6
7class RgbaColor(collections.namedtuple('RgbaColor', ['r', 'g', 'b', 'a'])):
8  """Encapsulates an RGBA color retrieved from an image."""
9  def __new__(cls, r, g, b, a=255):
10    return super(RgbaColor, cls).__new__(cls, r, g, b, a)
11
12  def __int__(self):
13    return (self.r << 16) | (self.g << 8) | self.b
14
15  def IsEqual(self, expected_color, tolerance=0):
16    """Verifies that the color is within a given tolerance of
17    the expected color."""
18    r_diff = abs(self.r - expected_color.r)
19    g_diff = abs(self.g - expected_color.g)
20    b_diff = abs(self.b - expected_color.b)
21    a_diff = abs(self.a - expected_color.a)
22    return (r_diff <= tolerance and g_diff <= tolerance
23        and b_diff <= tolerance and a_diff <= tolerance)
24
25  def AssertIsRGB(self, r, g, b, tolerance=0):
26    assert self.IsEqual(RgbaColor(r, g, b), tolerance)
27
28  def AssertIsRGBA(self, r, g, b, a, tolerance=0):
29    assert self.IsEqual(RgbaColor(r, g, b, a), tolerance)
30
31
32WEB_PAGE_TEST_ORANGE = RgbaColor(222, 100, 13)
33WHITE = RgbaColor(255, 255, 255)
34