Ejemplo n.º 1
0
def is_color(c):
    """
    Checks whether a value represents a color.

    As with Turtles, colors may be colormodel objects or strings.  They may also be
    sequences of 3 or 4 elements.  In the case of the latter, the elements of the
    sequence must all be in the range 0..1.

    :return: True if c represents a color
    :rtype:  ``bool``

    :param c: The value to test
    :type c:  any
    """
    import introcs
    if type(c) in [introcs.RGB, introcs.HSV]:
        return True

    if type(c) in [tuple, list] and 3 <= len(c) <= 4:
        from functools import reduce
        #print(reduce(lambda x, y: x and y, map(lambda z: type(z) in [int, float] and 0 <= z <= 1, c)))
        return reduce(
            lambda x, y: x and y,
            map(lambda z: type(z) in [int, float] and 0 <= z <= 1, c))

    return type(c) == str and (introcs.is_tkcolor(c) or introcs.is_webcolor(c))
Ejemplo n.º 2
0
def is_valid_color(c):
    """
    Returns: True c is a valid turtle color; False otherwise

    Parameter c: the value to check
    Precondition: NONE (c can be any value)
    """
    return (type(c) == introcs.RGB or type(c) == introcs.HSV or
            (type(c) == str and (introcs.is_tkcolor(c) or \
            introcs.is_webcolor(c))))