James Bennett avatar James Bennett committed 2650e73

And the other half of that commit, which somehow got eaten in the merge.

Comments (0)

Files changed (2)

 predefined name before returning a result in another format, this
 module always normalizes such values to be entirely lower-case.
 
+For colors specified via ``rgb()`` triplets, values contained in the
+triplets will be normalized via clipping in accordance with CSS:
+
+* Integer values less than 0 will be normalized to 0, and percentage
+  values less than 0% will be normalized to 0%.
+
+* Integer values greater than 255 will be normalized to 255, and
+  percentage values greater than 100% will be normalized to 100%.
+
+The functions :func:`normalize_integer_triplet` and
+:func:`normalize_percent_triplet` in this module can be used to
+perform this normalization manually if desired.
+
 For purposes of identifying the specification from which to draw the
 selection of defined color names, this module recognizes the following
 identifiers:
    :type hex_value: str
    :rtype: str
 
+.. function:: normalize_integer_triplet(rgb_triplet)
+
+    Normalize an integer ``rgb()`` triplet so that all values are
+    within the range 0-255 inclusive.
+
+    Examples::
+
+        >>> normalize_integer_triplet((128, 128, 128))
+        (128, 128, 128)
+        >>> normalize_integer_triplet((0, 0, 0))
+        (0, 0, 0)
+        >>> normalize_integer_triplet((255, 255, 255))
+        (255, 255, 255)
+        >>> normalize_integer_triplet((270, -20, 128))
+        (255, 0, 128)
+    
+    :param rgb_triplet: The integer ``rgb()`` triplet to normalize.
+    :type rgb_triplet: 3-tuple of integers
+    :rtype: 3-tuple of integers
+
+.. function:: normalize_percent_triplet(rgb_triplet)
+
+    Normalize a percentage ``rgb()`` triplet to that all values are
+    within the range 0%-100% inclusive.
+
+    Examples::
+
+        >>> normalize_percent_triplet(('50%', '50%', '50%'))
+        ('50%', '50%', '50%')
+        >>> normalize_percent_triplet(('0%', '100%', '0%'))
+        ('0%', '100%', '0%')
+        >>> normalize_percent_triplet(('-10%', '250%', '500%'))
+        ('0%', '100%', '100%')
+    
+    :param rgb_triplet: The percentage ``rgb()`` triplet to normalize.
+    :type rgb_triplet: 3-tuple of strings
+    :rtype: 3-tuple of strings
+
 
 Constants
 ~~~~~~~~~
 predefined name before returning a result in another format, this
 module always normalizes such values to be entirely lower-case.
 
+For colors specified via ``rgb()`` triplets, values contained in the
+triplets will be normalized via clipping in accordance with CSS:
+
+* Integer values less than 0 will be normalized to 0, and percentage
+  values less than 0% will be normalized to 0%.
+
+* Integer values greater than 255 will be normalized to 255, and
+  percentage values greater than 100% will be normalized to 100%.
+
+The functions :func:`normalize_integer_triplet` and
+:func:`normalize_percent_triplet` in this module can be used to
+perform this normalization manually if desired.
+
 For purposes of identifying the specification from which to draw the
 selection of defined color names, this module recognizes the following
 identifiers:
     return '#%s' % hex_digits.lower()
 
 
-def normalize_triplet(rgb_triplet):
+def normalize_integer_triplet(rgb_triplet):
     """
-    Normalize an ``rgb()`` triplet so that all values are within the
-    range 0-255 inclusive.
+    Normalize an integer ``rgb()`` triplet so that all values are
+    within the range 0-255 inclusive.
 
     Examples:
 
-    >>> normalize_triplet((128, 128, 128))
+    >>> normalize_integer_triplet((128, 128, 128))
     (128, 128, 128)
-    >>> normalize_triplet((0, 0, 0))
+    >>> normalize_integer_triplet((0, 0, 0))
     (0, 0, 0)
-    >>> normalize_triplet((255, 255, 255))
+    >>> normalize_integer_triplet((255, 255, 255))
     (255, 255, 255)
-    >>> normalize_triplet((270, -20, 128))
+    >>> normalize_integer_triplet((270, -20, 128))
     (255, 0, 128)
     
     """
-    return tuple([_normalize_rgb(value) for value in rgb_triplet])
+    return tuple([_normalize_integer_rgb(value) for value in rgb_triplet])
 
 
-def _normalize_rgb(value):
+def _normalize_integer_rgb(value):
     """
-    Normalize ``value`` for use in an ``rgb()`` triplet, as follows:
+    Normalize ``value`` for use in an integer ``rgb()`` triplet, as follows:
     
     * If ``value`` is less than 0, convert to 0.
     
 
     Examples:
 
-    >>> _normalize_rgb(0)
+    >>> _normalize_integer_rgb(0)
     0
-    >>> _normalize_rgb(255)
+    >>> _normalize_integer_rgb(255)
     255
-    >>> _normalize_rgb(128)
+    >>> _normalize_integer_rgb(128)
     128
-    >>> _normalize_rgb(-20)
+    >>> _normalize_integer_rgb(-20)
     0
-    >>> _normalize_rgb(270)
+    >>> _normalize_integer_rgb(270)
     255
     
     """
         return 0
     if value > 255:
         return 255
+
+
+def normalize_percent_triplet(rgb_triplet):
+    """
+    Normalize a percentage ``rgb()`` triplet to that all values are
+    within the range 0%-100% inclusive.
+
+    Examples:
+
+    >>> normalize_percent_triplet(('50%', '50%', '50%'))
+    ('50%', '50%', '50%')
+    >>> normalize_percent_triplet(('0%', '100%', '0%'))
+    ('0%', '100%', '0%')
+    >>> normalize_percent_triplet(('-10%', '250%', '500%'))
+    ('0%', '100%', '100%')
+    
+    """
+    return tuple([_normalize_percent_rgb(value) for value in rgb_triplet])
+    
+
+def _normalize_percent_rgb(value):
+    """
+    Normalize ``value`` for use in a percentage ``rgb()`` triplet, as
+    follows:
+
+    * If ``value`` is less than 0%, convert to 0%.
+
+    * If ``value`` is greater than 100%, convert to 100%.
+
+    Examples:
+
+    >>> _normalize_percent_rgb('0%')
+    '0%'
+    >>> _normalize_percent_rgb('100%')
+    '100%'
+    >>> _normalize_percent_rgb('62%')
+    '62%'
+    >>> _normalize_percent_rgb('-5%')
+    '0%'
+    >>> _normalize_percent_rgb('250%')
+    '100%'
+    >>> _normalize_percent_rgb('85.49%')
+    '85.49%'
+    
+    """
+    percent = value.split('%')[0]
+    percent = float(percent) if '.' in percent else int(percent)
+    
+    if 0 <= percent <= 100:
+        return '%s%%' % percent
+    if percent < 0:
+        return '0%'
+    if percent > 100:
+        return '100%'
     
 
 # Conversions from color names to various formats.
     'navy'
 
     """
-    return hex_to_name(rgb_to_hex(normalize_triplet(rgb_triplet)), spec=spec)
+    return hex_to_name(rgb_to_hex(normalize_integer_triplet(rgb_triplet)), spec=spec)
 
 
 def rgb_to_hex(rgb_triplet):
     '#000080'
 
     """
-    return '#%02x%02x%02x' % normalize_triplet(rgb_triplet)
+    return '#%02x%02x%02x' % normalize_integer_triplet(rgb_triplet)
 
 
 def rgb_to_rgb_percent(rgb_triplet):
     specials = {255: '100%', 128: '50%', 64: '25%',
                  32: '12.5%', 16: '6.25%', 0: '0%'}
     return tuple([specials.get(d, '%.02f%%' % ((d / 255.0) * 100)) \
-                  for d in normalize_triplet(rgb_triplet)])
+                  for d in normalize_integer_triplet(rgb_triplet)])
 
 
 # Conversions from percentage rgb() triplets to various formats.
     'goldenrod'
 
     """
-    return rgb_to_name(rgb_percent_to_rgb(rgb_percent_triplet), spec=spec)
+    return rgb_to_name(rgb_percent_to_rgb(normalize_percent_triplet(rgb_percent_triplet)), spec=spec)
 
 
 def rgb_percent_to_hex(rgb_percent_triplet):
     '#daa520'
 
     """
-    return rgb_to_hex(rgb_percent_to_rgb(rgb_percent_triplet))
+    return rgb_to_hex(rgb_percent_to_rgb(normalize_percent_triplet(rgb_percent_triplet)))
 
 
 def _percent_to_integer(percent):
     (218, 165, 32)
 
     """
-    return tuple(map(_percent_to_integer, rgb_percent_triplet))
+    return tuple(map(_percent_to_integer, normalize_percent_triplet(rgb_percent_triplet)))
 
 
 if __name__ == '__main__':
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.