src/reportlab/lib/colors.py
author rgbecker
Fri, 14 May 2010 15:59:06 +0000
changeset 3375 baeae60700c7
parent 3333 5f8d60b7e5de
child 3377 8122289973f5
permissions -rw-r--r--
colors.py: add CMYK color fading method & fix bug in scaling
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3290
6e5e33a15a3d allowed lexer to be varied
andy
parents: 3284
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2010
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 461
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2154
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/colors.py
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2154
diff changeset
     4
__version__=''' $Id$ '''
3029
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     5
__doc__='''Defines standard colour-handling classes and colour names.
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
     6
3029
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     7
We define standard classes to hold colours in two models:  RGB and CMYK.
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     8
These can be constructed from several popular formats.  We also include
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     9
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    10
- pre-built colour objects for the HTML standard colours
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    11
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    12
- pre-built colours used in ReportLab's branding
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    13
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    14
- various conversion and construction functions
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    15
'''
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
    16
import math
1061
11c13c203d02 Made import from utils absolute to avoid bombing Andy's yaml stuff
rgbecker
parents: 1041
diff changeset
    17
from reportlab.lib.utils import fp_str
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
    18
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
    19
class Color:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    20
    """This class is used to represent color.  Components red, green, blue
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    21
    are in the range 0 (dark) to 1 (full intensity)."""
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
    22
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    23
    def __init__(self, red=0, green=0, blue=0, alpha=1):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    24
        "Initialize with red, green, blue in range [0-1]."
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    25
        self.red = red
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    26
        self.green = green
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    27
        self.blue = blue
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    28
        self.alpha = alpha
563
4cdf14b5c761 Syncing with pingo
rgbecker
parents: 494
diff changeset
    29
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    30
    def __repr__(self):
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    31
        return "Color(%s)" % fp_str(*(self.red, self.green, self.blue,self.alpha)).replace(' ',',')
563
4cdf14b5c761 Syncing with pingo
rgbecker
parents: 494
diff changeset
    32
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    33
    def __hash__(self):
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    34
        return hash((self.red, self.green, self.blue, self.alpha))
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
    35
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    36
    def __cmp__(self,other):
3300
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    37
        '''simple comparison by component; cmyk != color ever
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    38
        >>> cmp(Color(0,0,0),None)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    39
        -1
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    40
        >>> cmp(Color(0,0,0),black)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    41
        0
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    42
        >>> cmp(Color(0,0,0),CMYKColor(0,0,0,1)),Color(0,0,0).rgba()==CMYKColor(0,0,0,1).rgba()
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    43
        (-1, True)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    44
        '''
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    45
        if isinstance(other,CMYKColor) or not isinstance(other,Color): return -1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    46
        try:
3300
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    47
            return cmp((self.red, self.green, self.blue, self.alpha),
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    48
                    (other.red, other.green, other.blue, other.alpha))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    49
        except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    50
            return -1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    51
        return 0
66
85e0ca12b6b2 Added ColorType
rgbecker
parents: 65
diff changeset
    52
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    53
    def rgb(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    54
        "Returns a three-tuple of components"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    55
        return (self.red, self.green, self.blue)
1663
1473a2b159d3 Added Color.hexval
rgbecker
parents: 1659
diff changeset
    56
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    57
    def rgba(self):
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    58
        "Returns a four-tuple of components"
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    59
        return (self.red, self.green, self.blue, self.alpha)
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    60
2001
49ed2369f56b Added bitmap_rgb function
rgbecker
parents: 1991
diff changeset
    61
    def bitmap_rgb(self):
2002
b9454d42cd64 Fix type problem
rgbecker
parents: 2001
diff changeset
    62
        return tuple(map(lambda x: int(x*255)&255, self.rgb()))
2001
49ed2369f56b Added bitmap_rgb function
rgbecker
parents: 1991
diff changeset
    63
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    64
    def bitmap_rgba(self):
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    65
        return tuple(map(lambda x: int(x*255)&255, self.rgba()))
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    66
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    67
    def hexval(self):
2001
49ed2369f56b Added bitmap_rgb function
rgbecker
parents: 1991
diff changeset
    68
        return '0x%02x%02x%02x' % self.bitmap_rgb()
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
    69
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    70
    def hexvala(self):
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    71
        return '0x%02x%02x%02x%02x' % self.bitmap_rgba()
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    72
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    73
    _cKwds='red green blue alpha'.split()
3225
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
    74
    def cKwds(self):
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
    75
        for k in self._cKwds:
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
    76
            yield k,getattr(self,k)
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
    77
    cKwds=property(cKwds)
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
    78
3155
e7deb49947dd colors.py: add clone method
rgbecker
parents: 3097
diff changeset
    79
    def clone(self,**kwds):
e7deb49947dd colors.py: add clone method
rgbecker
parents: 3097
diff changeset
    80
        '''copy then change values in kwds'''
3225
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
    81
        D = dict([kv for kv in self.cKwds])
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
    82
        D.update(kwds)
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
    83
        return self.__class__(**D)
3155
e7deb49947dd colors.py: add clone method
rgbecker
parents: 3097
diff changeset
    84
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
    85
class CMYKColor(Color):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    86
    """This represents colors using the CMYK (cyan, magenta, yellow, black)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    87
    model commonly used in professional printing.  This is implemented
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    88
    as a derived class so that renderers which only know about RGB "see it"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    89
    as an RGB color through its 'red','green' and 'blue' attributes, according
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    90
    to an approximate function.
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
    91
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    92
    The RGB approximation is worked out when the object in constructed, so
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    93
    the color attributes should not be changed afterwards.
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
    94
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    95
    Extra attributes may be attached to the class to support specific ink models,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    96
    and renderers may look for these."""
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
    97
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
    98
    _scale = 1.0
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    99
    def __init__(self, cyan=0, magenta=0, yellow=0, black=0,
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
   100
                spotName=None, density=1, knockout=None, alpha=1):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   101
        """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   102
        Initialize with four colors in range [0-1]. the optional
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   103
        spotName, density & knockout may be of use to specific renderers.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   104
        spotName is intended for use as an identifier to the renderer not client programs.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   105
        density is used to modify the overall amount of ink.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   106
        knockout is a renderer dependent option that determines whether the applied colour
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   107
        knocksout (removes) existing colour; None means use the global default.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   108
        """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   109
        self.cyan = cyan
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   110
        self.magenta = magenta
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   111
        self.yellow = yellow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   112
        self.black = black
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   113
        self.spotName = spotName
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   114
        self.density = max(min(density,1),0)    # force into right range
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   115
        self.knockout = knockout
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   116
        self.alpha = alpha
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
   117
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   118
        # now work out the RGB approximation. override
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   119
        self.red, self.green, self.blue = cmyk2rgb( (cyan, magenta, yellow, black) )
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
   120
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   121
        if density<1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   122
            #density adjustment of rgb approximants, effectively mix with white
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   123
            r, g, b = self.red, self.green, self.blue
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   124
            r = density*(r-1)+1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   125
            g = density*(g-1)+1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   126
            b = density*(b-1)+1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   127
            self.red, self.green, self.blue = (r,g,b)
1041
ca66f62767b5 Generalised CMYKColor added PCMYKColor
rgbecker
parents: 894
diff changeset
   128
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   129
    def __repr__(self):
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   130
        return "%s(%s%s%s%s%s)" % (self.__class__.__name__,
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
   131
            fp_str(self.cyan, self.magenta, self.yellow, self.black).replace(' ',','),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   132
            (self.spotName and (',spotName='+repr(self.spotName)) or ''),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   133
            (self.density!=1 and (',density='+fp_str(self.density)) or ''),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   134
            (self.knockout is not None and (',knockout=%d' % self.knockout) or ''),
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   135
            (self.alpha is not None and (',alpha=%d' % self.alpha) or ''),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   136
            )
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
   137
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   138
    def fader(self,n, reverse=False):
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   139
        '''return n colors based on density fade'''
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   140
        dd = self._scale/(n-1)
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   141
        L = [self.clone(density=i*dd) for i in xrange(n)]
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   142
        if reverse: L.reverse()
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   143
        return L
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   144
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   145
    def __hash__(self):
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   146
        return hash( (self.cyan, self.magenta, self.yellow, self.black, self.density, self.spotName, self.alpha) )
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
   147
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   148
    def __cmp__(self,other):
3300
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   149
        """obvious way to compare colours
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   150
        Comparing across the two color models is of limited use.
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   151
        >>> cmp(CMYKColor(0,0,0,1),None)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   152
        -1
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   153
        >>> cmp(CMYKColor(0,0,0,1),_CMYK_black)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   154
        0
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   155
        >>> cmp(PCMYKColor(0,0,0,100),_CMYK_black)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   156
        0
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   157
        >>> cmp(CMYKColor(0,0,0,1),Color(0,0,1)),Color(0,0,0).rgba()==CMYKColor(0,0,0,1).rgba()
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   158
        (-1, True)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   159
        """
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   160
        if not isinstance(other, CMYKColor): return -1
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   161
        try:
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   162
            return cmp(
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   163
                (self.cyan, self.magenta, self.yellow, self.black, self.density, self.alpha, self.spotName),
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   164
                (other.cyan, other.magenta, other.yellow, other.black, other.density, other.alpha, other.spotName))
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   165
        except: # or just return 'not equal' if not a color
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   166
            return -1
3300
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   167
        return 0
696
735615146ad7 Added CMYK support to core
andy_robinson
parents: 695
diff changeset
   168
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   169
    def cmyk(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   170
        "Returns a tuple of four color components - syntactic sugar"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   171
        return (self.cyan, self.magenta, self.yellow, self.black)
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
   172
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   173
    def cmyka(self):
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   174
        "Returns a tuple of five color components - syntactic sugar"
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
   175
        return (self.cyan, self.magenta, self.yellow, self.black, self.alpha)
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   176
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   177
    def _density_str(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   178
        return fp_str(self.density)
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   179
    _cKwds='cyan magenta yellow black density alpha spotName knockout'.split()
3225
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   180
1041
ca66f62767b5 Generalised CMYKColor added PCMYKColor
rgbecker
parents: 894
diff changeset
   181
class PCMYKColor(CMYKColor):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   182
    '''100 based CMYKColor with density and a spotName; just like Rimas uses'''
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   183
    _scale = 100.
3292
2719247a42cd colors.py: use 0-100 for alpha in PCMYK colors
rgbecker
parents: 3290
diff changeset
   184
    def __init__(self,cyan,magenta,yellow,black,density=100,spotName=None,knockout=None,alpha=100):
2719247a42cd colors.py: use 0-100 for alpha in PCMYK colors
rgbecker
parents: 3290
diff changeset
   185
        CMYKColor.__init__(self,cyan/100.,magenta/100.,yellow/100.,black/100.,spotName,density/100.,knockout=knockout,alpha=alpha/100.)
1041
ca66f62767b5 Generalised CMYKColor added PCMYKColor
rgbecker
parents: 894
diff changeset
   186
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   187
    def __repr__(self):
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   188
        return "%s(%s%s%s%s%s)" % (self.__class__.__name__,
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
   189
            fp_str(self.cyan*100, self.magenta*100, self.yellow*100, self.black*100).replace(' ',','),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   190
            (self.spotName and (',spotName='+repr(self.spotName)) or ''),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   191
            (self.density!=1 and (',density='+fp_str(self.density*100)) or ''),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   192
            (self.knockout is not None and (',knockout=%d' % self.knockout) or ''),
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   193
            (self.alpha is not None and (',alpha=%s' % (fp_str(self.alpha*100))) or ''),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   194
            )
1041
ca66f62767b5 Generalised CMYKColor added PCMYKColor
rgbecker
parents: 894
diff changeset
   195
3225
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   196
    def cKwds(self):
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   197
        K=self._cKwds
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   198
        S=K[:6]
3225
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   199
        for k in self._cKwds:
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   200
            v=getattr(self,k)
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   201
            if k in S: v*=100
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   202
            yield k,v
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   203
    cKwds=property(cKwds)
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   204
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
   205
class CMYKColorSep(CMYKColor):
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
   206
    '''special case color for making separating pdfs'''
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   207
    _scale = 1.
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
   208
    def __init__(self, cyan=0, magenta=0, yellow=0, black=0,
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
   209
                spotName=None, density=1,alpha=1):
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   210
        CMYKColor.__init__(self,cyan,magenta,yellow,black,spotName,density,knockout=None,alpha=alpha)
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   211
    _cKwds='cyan magenta yellow black density alpha spotName'.split()
3225
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   212
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   213
class PCMYKColorSep(PCMYKColor,CMYKColorSep):
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   214
    '''special case color for making separating pdfs'''
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   215
    _scale = 100.
3225
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   216
    def __init__(self, cyan=0, magenta=0, yellow=0, black=0,
3292
2719247a42cd colors.py: use 0-100 for alpha in PCMYK colors
rgbecker
parents: 3290
diff changeset
   217
                spotName=None, density=100, alpha=100):
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   218
        PCMYKColor.__init__(self,cyan,magenta,yellow,black,density,spotName,knockout=None,alpha=alpha)
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   219
    _cKwds='cyan magenta yellow black density alpha spotName'.split()
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
   220
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3300
diff changeset
   221
def cmyk2rgb(cmyk,density=1):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   222
    "Convert from a CMYK color tuple to an RGB color tuple"
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3300
diff changeset
   223
    c,m,y,k = cmyk
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   224
    # From the Adobe Postscript Ref. Manual 2nd ed.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   225
    r = 1.0 - min(1.0, c + k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   226
    g = 1.0 - min(1.0, m + k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   227
    b = 1.0 - min(1.0, y + k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   228
    return (r,g,b)
51
6ebbabd7abf1 Sync with pingo
rgbecker
parents: 42
diff changeset
   229
430
f5c0a69cdcd9 added rgb2cmyk
rgbecker
parents: 270
diff changeset
   230
def rgb2cmyk(r,g,b):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   231
    '''one way to get cmyk from rgb'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   232
    c = 1 - r
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   233
    m = 1 - g
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   234
    y = 1 - b
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   235
    k = min(c,m,y)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   236
    c = min(1,max(0,c-k))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   237
    m = min(1,max(0,m-k))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   238
    y = min(1,max(0,y-k))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   239
    k = min(1,max(0,k))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   240
    return (c,m,y,k)
430
f5c0a69cdcd9 added rgb2cmyk
rgbecker
parents: 270
diff changeset
   241
641
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   242
def color2bw(colorRGB):
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   243
    "Transform an RGB color to a black and white equivalent."
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   244
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   245
    col = colorRGB
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   246
    r, g, b, a = col.red, col.green, col.blue, col.alpha
641
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   247
    n = (r + g + b) / 3.0
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   248
    bwColorRGB = Color(n, n, n, a)
641
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   249
    return bwColorRGB
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   250
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   251
def HexColor(val, htmlOnly=False, alpha=False):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   252
    """This function converts a hex string, or an actual integer number,
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   253
    into the corresponding color.  E.g., in "#AABBCC" or 0xAABBCC,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   254
    AA is the red, BB is the green, and CC is the blue (00-FF).
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   255
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   256
    An alpha value can also be given in the form #AABBCCDD or 0xAABBCCDD where
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   257
    DD is the alpha value.
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   258
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   259
    For completeness I assume that #aabbcc or 0xaabbcc are hex numbers
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   260
    otherwise a pure integer is converted as decimal rgb.  If htmlOnly is true,
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   261
    only the #aabbcc form is allowed.
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   262
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   263
    >>> HexColor('#ffffff')
3295
66da176a4a4f Fixed colors doctests
damian
parents: 3292
diff changeset
   264
    Color(1,1,1,1)
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   265
    >>> HexColor('#FFFFFF')
3295
66da176a4a4f Fixed colors doctests
damian
parents: 3292
diff changeset
   266
    Color(1,1,1,1)
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   267
    >>> HexColor('0xffffff')
3295
66da176a4a4f Fixed colors doctests
damian
parents: 3292
diff changeset
   268
    Color(1,1,1,1)
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   269
    >>> HexColor('16777215')
3295
66da176a4a4f Fixed colors doctests
damian
parents: 3292
diff changeset
   270
    Color(1,1,1,1)
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   271
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   272
    An '0x' or '#' prefix is required for hex (as opposed to decimal):
65
fb9c613d4c13 Allow 0x in HexColor, added stringToColor, fixed caching in getAllNamedColors
rgbecker
parents: 51
diff changeset
   273
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   274
    >>> HexColor('ffffff')
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   275
    Traceback (most recent call last):
3295
66da176a4a4f Fixed colors doctests
damian
parents: 3292
diff changeset
   276
    ValueError: invalid literal for int() with base 10: 'ffffff'
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   277
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   278
    >>> HexColor('#FFFFFF', htmlOnly=True)
3295
66da176a4a4f Fixed colors doctests
damian
parents: 3292
diff changeset
   279
    Color(1,1,1,1)
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   280
    >>> HexColor('0xffffff', htmlOnly=True)
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   281
    Traceback (most recent call last):
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   282
    ValueError: not a hex string
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   283
    >>> HexColor('16777215', htmlOnly=True)
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   284
    Traceback (most recent call last):
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   285
    ValueError: not a hex string
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   286
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   287
    """ #" for emacs
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   288
3176
c15bdeba0afc colors.py: allow unicode arguments in toColor and elsewhere
rgbecker
parents: 3155
diff changeset
   289
    if isinstance(val,basestring):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   290
        b = 10
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   291
        if val[:1] == '#':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   292
            val = val[1:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   293
            b = 16
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   294
            if len(val) == 8:
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   295
                alpha = True
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   296
        else:
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   297
            if htmlOnly:
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   298
                raise ValueError('not a hex string')
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
   299
            if val[:2].lower() == '0x':
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   300
                b = 16
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   301
                val = val[2:]
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   302
                if len(val) == 8:
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   303
                    alpha = True
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
   304
        val = int(val,b)
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   305
    if alpha:
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   306
        return Color((val>>24)&0xFF/255.0,((val>>16)&0xFF)/255.0,((val>>8)&0xFF)/255.0,(val&0xFF)/255.0)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   307
    return Color(((val>>16)&0xFF)/255.0,((val>>8)&0xFF)/255.0,(val&0xFF)/255.0)
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   308
1154
c6314e34823b Added method to color for interpolating colors - changed shadedRect in
johnprecedo
parents: 1120
diff changeset
   309
def linearlyInterpolatedColor(c0, c1, x0, x1, x):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   310
    """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   311
    Linearly interpolates colors. Can handle RGB, CMYK and PCMYK
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   312
    colors - give ValueError if colours aren't the same.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   313
    Doesn't currently handle 'Spot Color Interpolation'.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   314
    """
1154
c6314e34823b Added method to color for interpolating colors - changed shadedRect in
johnprecedo
parents: 1120
diff changeset
   315
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   316
    if c0.__class__ != c1.__class__:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   317
        raise ValueError, "Color classes must be the same for interpolation!"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   318
    if x1<x0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   319
        x0,x1,c0,c1 = x1,x0,c1,c0 # normalized so x1>x0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   320
    if x<x0-1e-8 or x>x1+1e-8: # fudge factor for numerical problems
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   321
        raise ValueError, "Can't interpolate: x=%f is not between %f and %f!" % (x,x0,x1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   322
    if x<=x0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   323
        return c0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   324
    elif x>=x1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   325
        return c1
1154
c6314e34823b Added method to color for interpolating colors - changed shadedRect in
johnprecedo
parents: 1120
diff changeset
   326
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   327
    cname = c0.__class__.__name__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   328
    dx = float(x1-x0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   329
    x = x-x0
1154
c6314e34823b Added method to color for interpolating colors - changed shadedRect in
johnprecedo
parents: 1120
diff changeset
   330
2047
7ff0f67de98e jython compatibility hack
dragan1
parents: 2032
diff changeset
   331
    if cname == 'Color': # RGB
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   332
        r = c0.red+x*(c1.red - c0.red)/dx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   333
        g = c0.green+x*(c1.green- c0.green)/dx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   334
        b = c0.blue+x*(c1.blue - c0.blue)/dx
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
   335
        a = c0.alpha+x*(c1.alpha - c0.alpha)/dx
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
   336
        return Color(r,g,b,alpha=a)
2047
7ff0f67de98e jython compatibility hack
dragan1
parents: 2032
diff changeset
   337
    elif cname == 'CMYKColor':
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   338
        c = c0.cyan+x*(c1.cyan - c0.cyan)/dx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   339
        m = c0.magenta+x*(c1.magenta - c0.magenta)/dx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   340
        y = c0.yellow+x*(c1.yellow - c0.yellow)/dx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   341
        k = c0.black+x*(c1.black - c0.black)/dx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   342
        d = c0.density+x*(c1.density - c0.density)/dx
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
   343
        a = c0.alpha+x*(c1.alpha - c0.alpha)/dx
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
   344
        return CMYKColor(c,m,y,k, density=d, alpha=a)
2047
7ff0f67de98e jython compatibility hack
dragan1
parents: 2032
diff changeset
   345
    elif cname == 'PCMYKColor':
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   346
        if cmykDistance(c0,c1)<1e-8:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   347
            #colors same do density and preserve spotName if any
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   348
            assert c0.spotName == c1.spotName, "Identical cmyk, but different spotName"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   349
            c = c0.cyan
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   350
            m = c0.magenta
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   351
            y = c0.yellow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   352
            k = c0.black
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   353
            d = c0.density+x*(c1.density - c0.density)/dx
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   354
            return PCMYKColor(c*100,m*100,y*100,k*100, density=d*100,
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   355
                              spotName=c0.spotName, alpha=c0.alpha)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   356
        elif cmykDistance(c0,_CMYK_white)<1e-8:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   357
            #special c0 is white
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   358
            c = c1.cyan
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   359
            m = c1.magenta
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   360
            y = c1.yellow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   361
            k = c1.black
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   362
            d = x*c1.density/dx
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   363
            return PCMYKColor(c*100,m*100,y*100,k*100, density=d*100,
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   364
                              spotName=c1.spotName, alpha=c1.alpha)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   365
        elif cmykDistance(c1,_CMYK_white)<1e-8:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   366
            #special c1 is white
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   367
            c = c0.cyan
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   368
            m = c0.magenta
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   369
            y = c0.yellow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   370
            k = c0.black
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   371
            d = x*c0.density/dx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   372
            d = c0.density*(1-x/dx)
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   373
            return PCMYKColor(c*100,m*100,y*100,k*100, density=d*100,
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   374
                              spotName=c0.spotName, alpha=c0.alpha)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   375
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   376
            c = c0.cyan+x*(c1.cyan - c0.cyan)/dx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   377
            m = c0.magenta+x*(c1.magenta - c0.magenta)/dx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   378
            y = c0.yellow+x*(c1.yellow - c0.yellow)/dx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   379
            k = c0.black+x*(c1.black - c0.black)/dx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   380
            d = c0.density+x*(c1.density - c0.density)/dx
3284
dcb7eb12976d colors.py: fix mis-spelt reference
rgbecker
parents: 3282
diff changeset
   381
            a = c0.alpha+x*(c1.alpha - c0.alpha)/dx
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
   382
            return PCMYKColor(c*100,m*100,y*100,k*100, density=d*100, alpha=a)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   383
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   384
        raise ValueError, "Can't interpolate: Unknown color class %s!" % cname
1154
c6314e34823b Added method to color for interpolating colors - changed shadedRect in
johnprecedo
parents: 1120
diff changeset
   385
3254
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   386
def obj_R_G_B(c):
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   387
    '''attempt to convert an object to (red,green,blue)'''
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   388
    if isinstance(c,Color):
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   389
        return c.red,c.green,c.blue
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   390
    elif isinstance(c,(tuple,list)):
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   391
        if len(c)==3:
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   392
            return tuple(c)
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   393
        elif len(c)==4:
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   394
            return toColor(c).rgb()
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   395
        else:
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   396
            raise ValueError('obj_R_G_B(%r) bad argument' % (c))
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   397
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   398
# special case -- indicates no drawing should be done
696
735615146ad7 Added CMYK support to core
andy_robinson
parents: 695
diff changeset
   399
# this is a hangover from PIDDLE - suggest we ditch it since it is not used anywhere
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   400
transparent = Color(0,0,0,alpha=0)
563
4cdf14b5c761 Syncing with pingo
rgbecker
parents: 494
diff changeset
   401
1308
da76452fcd92 Forgot _CMYK_white again
rgbecker
parents: 1307
diff changeset
   402
_CMYK_white=CMYKColor(0,0,0,0)
1659
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   403
_PCMYK_white=PCMYKColor(0,0,0,0)
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   404
_CMYK_black=CMYKColor(0,0,0,1)
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   405
_PCMYK_black=PCMYKColor(0,0,0,100)
1307
73a24d4d6aba Forgot _CMYK_white
rgbecker
parents: 1306
diff changeset
   406
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   407
# Special colors
2154
03798f09cb1f Fix to use latest blue
rgbecker
parents: 2136
diff changeset
   408
ReportLabBlueOLD = HexColor(0x4e5688)
03798f09cb1f Fix to use latest blue
rgbecker
parents: 2136
diff changeset
   409
ReportLabBlue = HexColor(0x00337f)
03798f09cb1f Fix to use latest blue
rgbecker
parents: 2136
diff changeset
   410
ReportLabBluePCMYK = PCMYKColor(100,65,0,30,spotName='Pantone 288U')
1429
6fd172dc8d64 Added ReportLabLightBlue.
dinu_gherman
parents: 1310
diff changeset
   411
ReportLabLightBlue = HexColor(0xb7b9d3)
1844
9ad0785bb912 Added special reportLabFid colours
rgbecker
parents: 1727
diff changeset
   412
ReportLabFidBlue=HexColor(0x3366cc)
9ad0785bb912 Added special reportLabFid colours
rgbecker
parents: 1727
diff changeset
   413
ReportLabFidRed=HexColor(0xcc0033)
1991
1c4729336cb6 added ReportLabGreen
fuzzypuffin
parents: 1844
diff changeset
   414
ReportLabGreen = HexColor(0x336600)
1c4729336cb6 added ReportLabGreen
fuzzypuffin
parents: 1844
diff changeset
   415
ReportLabLightGreen = HexColor(0x339933)
894
096027d2aa04 Added special color - ReportLabBlue.
johnprecedo
parents: 696
diff changeset
   416
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   417
# color constants -- mostly from HTML standard
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   418
aliceblue =     HexColor(0xF0F8FF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   419
antiquewhite =  HexColor(0xFAEBD7)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   420
aqua =  HexColor(0x00FFFF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   421
aquamarine =    HexColor(0x7FFFD4)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   422
azure =     HexColor(0xF0FFFF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   423
beige =     HexColor(0xF5F5DC)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   424
bisque =    HexColor(0xFFE4C4)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   425
black =     HexColor(0x000000)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   426
blanchedalmond =    HexColor(0xFFEBCD)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   427
blue =  HexColor(0x0000FF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   428
blueviolet =    HexColor(0x8A2BE2)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   429
brown =     HexColor(0xA52A2A)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   430
burlywood =     HexColor(0xDEB887)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   431
cadetblue =     HexColor(0x5F9EA0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   432
chartreuse =    HexColor(0x7FFF00)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   433
chocolate =     HexColor(0xD2691E)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   434
coral =     HexColor(0xFF7F50)
1727
5125562d1372 Add cornflowerblue = cornflower
rgbecker
parents: 1723
diff changeset
   435
cornflowerblue = cornflower =   HexColor(0x6495ED)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   436
cornsilk =  HexColor(0xFFF8DC)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   437
crimson =   HexColor(0xDC143C)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   438
cyan =  HexColor(0x00FFFF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   439
darkblue =  HexColor(0x00008B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   440
darkcyan =  HexColor(0x008B8B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   441
darkgoldenrod =     HexColor(0xB8860B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   442
darkgray =  HexColor(0xA9A9A9)
3097
9c90e1e4baaa colors.py: normalize spelling of grey/gray colours, suggested by Paul Barass
rgbecker
parents: 3029
diff changeset
   443
darkgrey =  darkgray
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   444
darkgreen =     HexColor(0x006400)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   445
darkkhaki =     HexColor(0xBDB76B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   446
darkmagenta =   HexColor(0x8B008B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   447
darkolivegreen =    HexColor(0x556B2F)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   448
darkorange =    HexColor(0xFF8C00)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   449
darkorchid =    HexColor(0x9932CC)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   450
darkred =   HexColor(0x8B0000)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   451
darksalmon =    HexColor(0xE9967A)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   452
darkseagreen =  HexColor(0x8FBC8B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   453
darkslateblue =     HexColor(0x483D8B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   454
darkslategray =     HexColor(0x2F4F4F)
3097
9c90e1e4baaa colors.py: normalize spelling of grey/gray colours, suggested by Paul Barass
rgbecker
parents: 3029
diff changeset
   455
darkslategrey = darkslategray
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   456
darkturquoise =     HexColor(0x00CED1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   457
darkviolet =    HexColor(0x9400D3)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   458
deeppink =  HexColor(0xFF1493)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   459
deepskyblue =   HexColor(0x00BFFF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   460
dimgray =   HexColor(0x696969)
3097
9c90e1e4baaa colors.py: normalize spelling of grey/gray colours, suggested by Paul Barass
rgbecker
parents: 3029
diff changeset
   461
dimgrey = dimgray
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   462
dodgerblue =    HexColor(0x1E90FF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   463
firebrick =     HexColor(0xB22222)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   464
floralwhite =   HexColor(0xFFFAF0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   465
forestgreen =   HexColor(0x228B22)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   466
fuchsia =   HexColor(0xFF00FF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   467
gainsboro =     HexColor(0xDCDCDC)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   468
ghostwhite =    HexColor(0xF8F8FF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   469
gold =  HexColor(0xFFD700)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   470
goldenrod =     HexColor(0xDAA520)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   471
gray =  HexColor(0x808080)
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   472
grey = gray
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   473
green =     HexColor(0x008000)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   474
greenyellow =   HexColor(0xADFF2F)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   475
honeydew =  HexColor(0xF0FFF0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   476
hotpink =   HexColor(0xFF69B4)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   477
indianred =     HexColor(0xCD5C5C)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   478
indigo =    HexColor(0x4B0082)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   479
ivory =     HexColor(0xFFFFF0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   480
khaki =     HexColor(0xF0E68C)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   481
lavender =  HexColor(0xE6E6FA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   482
lavenderblush =     HexColor(0xFFF0F5)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   483
lawngreen =     HexColor(0x7CFC00)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   484
lemonchiffon =  HexColor(0xFFFACD)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   485
lightblue =     HexColor(0xADD8E6)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   486
lightcoral =    HexColor(0xF08080)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   487
lightcyan =     HexColor(0xE0FFFF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   488
lightgoldenrodyellow =  HexColor(0xFAFAD2)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   489
lightgreen =    HexColor(0x90EE90)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   490
lightgrey =     HexColor(0xD3D3D3)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   491
lightpink =     HexColor(0xFFB6C1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   492
lightsalmon =   HexColor(0xFFA07A)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   493
lightseagreen =     HexColor(0x20B2AA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   494
lightskyblue =  HexColor(0x87CEFA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   495
lightslategray =    HexColor(0x778899)
3097
9c90e1e4baaa colors.py: normalize spelling of grey/gray colours, suggested by Paul Barass
rgbecker
parents: 3029
diff changeset
   496
lightslategrey = lightslategray
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   497
lightsteelblue =    HexColor(0xB0C4DE)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   498
lightyellow =   HexColor(0xFFFFE0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   499
lime =  HexColor(0x00FF00)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   500
limegreen =     HexColor(0x32CD32)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   501
linen =     HexColor(0xFAF0E6)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   502
magenta =   HexColor(0xFF00FF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   503
maroon =    HexColor(0x800000)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   504
mediumaquamarine =  HexColor(0x66CDAA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   505
mediumblue =    HexColor(0x0000CD)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   506
mediumorchid =  HexColor(0xBA55D3)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   507
mediumpurple =  HexColor(0x9370DB)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   508
mediumseagreen =    HexColor(0x3CB371)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   509
mediumslateblue =   HexColor(0x7B68EE)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   510
mediumspringgreen =     HexColor(0x00FA9A)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   511
mediumturquoise =   HexColor(0x48D1CC)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   512
mediumvioletred =   HexColor(0xC71585)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   513
midnightblue =  HexColor(0x191970)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   514
mintcream =     HexColor(0xF5FFFA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   515
mistyrose =     HexColor(0xFFE4E1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   516
moccasin =  HexColor(0xFFE4B5)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   517
navajowhite =   HexColor(0xFFDEAD)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   518
navy =  HexColor(0x000080)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   519
oldlace =   HexColor(0xFDF5E6)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   520
olive =     HexColor(0x808000)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   521
olivedrab =     HexColor(0x6B8E23)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   522
orange =    HexColor(0xFFA500)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   523
orangered =     HexColor(0xFF4500)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   524
orchid =    HexColor(0xDA70D6)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   525
palegoldenrod =     HexColor(0xEEE8AA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   526
palegreen =     HexColor(0x98FB98)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   527
paleturquoise =     HexColor(0xAFEEEE)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   528
palevioletred =     HexColor(0xDB7093)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   529
papayawhip =    HexColor(0xFFEFD5)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   530
peachpuff =     HexColor(0xFFDAB9)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   531
peru =  HexColor(0xCD853F)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   532
pink =  HexColor(0xFFC0CB)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   533
plum =  HexColor(0xDDA0DD)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   534
powderblue =    HexColor(0xB0E0E6)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   535
purple =    HexColor(0x800080)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   536
red =   HexColor(0xFF0000)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   537
rosybrown =     HexColor(0xBC8F8F)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   538
royalblue =     HexColor(0x4169E1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   539
saddlebrown =   HexColor(0x8B4513)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   540
salmon =    HexColor(0xFA8072)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   541
sandybrown =    HexColor(0xF4A460)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   542
seagreen =  HexColor(0x2E8B57)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   543
seashell =  HexColor(0xFFF5EE)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   544
sienna =    HexColor(0xA0522D)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   545
silver =    HexColor(0xC0C0C0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   546
skyblue =   HexColor(0x87CEEB)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   547
slateblue =     HexColor(0x6A5ACD)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   548
slategray =     HexColor(0x708090)
3097
9c90e1e4baaa colors.py: normalize spelling of grey/gray colours, suggested by Paul Barass
rgbecker
parents: 3029
diff changeset
   549
slategrey = slategray
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   550
snow =  HexColor(0xFFFAFA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   551
springgreen =   HexColor(0x00FF7F)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   552
steelblue =     HexColor(0x4682B4)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   553
tan =   HexColor(0xD2B48C)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   554
teal =  HexColor(0x008080)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   555
thistle =   HexColor(0xD8BFD8)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   556
tomato =    HexColor(0xFF6347)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   557
turquoise =     HexColor(0x40E0D0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   558
violet =    HexColor(0xEE82EE)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   559
wheat =     HexColor(0xF5DEB3)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   560
white =     HexColor(0xFFFFFF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   561
whitesmoke =    HexColor(0xF5F5F5)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   562
yellow =    HexColor(0xFFFF00)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   563
yellowgreen =   HexColor(0x9ACD32)
1654
eaf787730479 Added setColors fixed toColor
rgbecker
parents: 1481
diff changeset
   564
fidblue=HexColor(0x3366cc)
2490
49f8090be0f8 colors.py: add a fidred
rgbecker
parents: 2402
diff changeset
   565
fidred=HexColor(0xcc0033)
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2392
diff changeset
   566
fidlightblue=HexColor("#d6e0f5")
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   567
66
85e0ca12b6b2 Added ColorType
rgbecker
parents: 65
diff changeset
   568
ColorType=type(black)
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   569
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   570
    ################################################################
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   571
    #
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   572
    #  Helper functions for dealing with colors.  These tell you
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   573
    #  which are predefined, so you can print color charts;
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   574
    #  and can give the nearest match to an arbitrary color object
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   575
    #
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   576
    #################################################################
563
4cdf14b5c761 Syncing with pingo
rgbecker
parents: 494
diff changeset
   577
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   578
def colorDistance(col1, col2):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   579
    """Returns a number between 0 and root(3) stating how similar
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   580
    two colours are - distance in r,g,b, space.  Only used to find
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   581
    names for things."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   582
    return math.sqrt(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   583
            (col1.red - col2.red)**2 +
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   584
            (col1.green - col2.green)**2 +
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   585
            (col1.blue - col2.blue)**2
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   586
            )
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   587
1306
cd0441305584 Improved interpolation added cmykDistance
rgbecker
parents: 1221
diff changeset
   588
def cmykDistance(col1, col2):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   589
    """Returns a number between 0 and root(4) stating how similar
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   590
    two colours are - distance in r,g,b, space.  Only used to find
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   591
    names for things."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   592
    return math.sqrt(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   593
            (col1.cyan - col2.cyan)**2 +
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   594
            (col1.magenta - col2.magenta)**2 +
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   595
            (col1.yellow - col2.yellow)**2 +
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   596
            (col1.black - col2.black)**2
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   597
            )
1306
cd0441305584 Improved interpolation added cmykDistance
rgbecker
parents: 1221
diff changeset
   598
67
a2da44ccdb34 Fixed getAllNamedColors, needed global etc
rgbecker
parents: 66
diff changeset
   599
_namedColors = None
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   600
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   601
def getAllNamedColors():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   602
    #returns a dictionary of all the named ones in the module
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   603
    # uses a singleton for efficiency
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   604
    global _namedColors
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   605
    if _namedColors is not None: return _namedColors
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   606
    import colors
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   607
    _namedColors = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   608
    for (name, value) in colors.__dict__.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   609
        if isinstance(value, Color):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   610
            _namedColors[name] = value
65
fb9c613d4c13 Allow 0x in HexColor, added stringToColor, fixed caching in getAllNamedColors
rgbecker
parents: 51
diff changeset
   611
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   612
    return _namedColors
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   613
461
228b00fb140a Fix for sun compiler
rgbecker
parents: 431
diff changeset
   614
def describe(aColor,mode=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   615
    '''finds nearest colour match to aColor.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   616
    mode=0 print a string desription
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   617
    mode=1 return a string description
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   618
    mode=2 return (distance, colorName)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   619
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   620
    namedColors = getAllNamedColors()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   621
    closest = (10, None, None)  #big number, name, color
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   622
    for (name, color) in namedColors.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   623
        distance = colorDistance(aColor, color)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   624
        if distance < closest[0]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   625
            closest = (distance, name, color)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   626
    if mode<=1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   627
        s = 'best match is %s, distance %0.4f' % (closest[1], closest[0])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   628
        if mode==0: print s
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   629
        else: return s
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   630
    elif mode==2:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   631
        return (closest[1], closest[0])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   632
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   633
        raise ValueError, "Illegal value for mode "+str(mode)
65
fb9c613d4c13 Allow 0x in HexColor, added stringToColor, fixed caching in getAllNamedColors
rgbecker
parents: 51
diff changeset
   634
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   635
def hue2rgb(m1, m2, h):
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   636
    if h<0: h += 1
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   637
    if h>1: h -= 1
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   638
    if h*6<1: return m1+(m2-m1)*h*6
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   639
    if h*2<1: return m2
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   640
    if h*3<2: return m1+(m2-m1)*(4-6*h)
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   641
    return m1
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   642
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   643
def hsl2rgb(h, s, l): 
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   644
    if l<=0.5:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   645
        m2 = l*(s+1)
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   646
    else:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   647
        m2 = l+s-l*s
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   648
    m1 = l*2-m2
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   649
    return hue2rgb(m1, m2, h+1./3),hue2rgb(m1, m2, h),hue2rgb(m1, m2, h-1./3)
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   650
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   651
class cssParse:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   652
    def pcVal(self,v):
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   653
        v = v.strip()
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   654
        try:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   655
            c=eval(v[:-1])
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   656
            if not isinstance(c,(float,int)): raise ValueError
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   657
            c=min(100,max(0,c))/100.
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   658
        except:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   659
            raise ValueError('bad percentage argument value %r in css color %r' % (v,self.s))
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   660
        return c
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   661
3297
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   662
    def rgbPcVal(self,v):
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   663
        return int(self.pcVal(v)*255+0.5)/255.
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   664
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   665
    def rgbVal(self,v):
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   666
        v = v.strip()
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   667
        try:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   668
            c=eval(v[:])
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   669
            if not isinstance(c,int): raise ValueError
3297
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   670
            return int(min(255,max(0,c)))/255.
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   671
        except:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   672
            raise ValueError('bad argument value %r in css color %r' % (v,self.s))
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   673
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   674
    def hueVal(self,v):
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   675
        v = v.strip()
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   676
        try:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   677
            c=eval(v[:])
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   678
            if not isinstance(c,(int,float)): raise ValueError
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   679
            return ((c%360+360)%360)/360.
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   680
        except:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   681
            raise ValueError('bad hue argument value %r in css color %r' % (v,self.s))
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   682
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   683
    def alphaVal(self,v,c=1,n='alpha'):
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   684
        try:
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   685
            a = eval(v.strip())
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   686
            if not isinstance(a,(int,float)): raise ValueError
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   687
            return min(c,max(0,a))
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   688
        except:
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   689
            raise ValueError('bad %s argument value %r in css color %r' % (n,v,self.s))
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   690
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   691
    def __call__(self,s):
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   692
        s = s.strip()
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   693
        hsl = s.startswith('hsl')
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   694
        rgb = s.startswith('rgb')
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   695
        cmyk = s.startswith('cmyk')
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   696
        c = 1
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   697
        if hsl: n = 3
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   698
        if rgb: n = 3
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   699
        if cmyk:
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   700
            n = 4
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   701
        else:
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   702
            cmyk = s.startswith('pcmyk')
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   703
            if cmyk:
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   704
                n = 5
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   705
                c = 100
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   706
        if not (rgb or hsl or cmyk): return None
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   707
        self.s = s
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   708
        n = s[n:]
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   709
        ha = n.startswith('a')
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   710
        n = n[(ha and 1 or 0):].strip()
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   711
        if not n.startswith('(') or not n.endswith(')'):
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   712
            raise ValueError('improperly formatted css style color %r' % s)
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   713
        n = n[1:-1].split(',')  #strip parens and split on comma
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   714
        a = len(n)
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   715
        b = cmyk and 4 or 3
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   716
        if ha and a!=(b+1) or not ha and a!=b:
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   717
            raise ValueError('css color %r has wrong number of components' % s)
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   718
        if ha:
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   719
            n,a = n[:b],self.alphaVal(n[b],c)
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   720
        else:
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   721
            a = c
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   722
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   723
        if cmyk:
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   724
            C = self.alphaVal(n[0],c,'cyan')
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   725
            M = self.alphaVal(n[1],c,'magenta')
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   726
            Y = self.alphaVal(n[2],c,'yellow')
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   727
            K = self.alphaVal(n[3],c,'black')
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   728
            return (c>1 and PCMYKColor or CMYKColor)(C,M,Y,K,alpha=a)
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   729
        else:
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   730
            if hsl:
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   731
                R,G,B= hsl2rgb(self.hueVal(n[0]),self.pcVal(n[1]),self.pcVal(n[2]))
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   732
            else:
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   733
                R,G,B = map('%' in n[0] and self.rgbPcVal or self.rgbVal,n)
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   734
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   735
            return Color(R,G,B,a)
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   736
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   737
cssParse=cssParse()
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   738
246
1d29db1962fc stringToColor-->toColor
rgbecker
parents: 89
diff changeset
   739
def toColor(arg,default=None):
3297
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   740
    '''try to map an arbitrary arg to a color instance
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   741
    >>> toColor('rgb(128,0,0)')==toColor('rgb(50%,0%,0%)')
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   742
    True
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   743
    >>> toColor('rgb(50%,0%,0%)')!=Color(0.5,0,0,1)
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   744
    True
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   745
    >>> toColor('hsl(0,100%,50%)')==toColor('rgb(255,0,0)')
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   746
    True
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   747
    >>> toColor('hsl(-120,100%,50%)')==toColor('rgb(0,0,255)')
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   748
    True
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   749
    >>> toColor('hsl(120,100%,50%)')==toColor('rgb(0,255,0)')
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   750
    True
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   751
    >>> toColor('rgba(255,0,0,0.5)')==Color(1,0,0,0.5)
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   752
    True
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   753
    >>> toColor('cmyk(1,0,0,0)')==CMYKColor(1,0,0,0)
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   754
    True
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   755
    >>> toColor('pcmyk(100,0,0,0)')==PCMYKColor(100,0,0,0)
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   756
    True
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   757
    >>> toColor('cmyka(1,0,0,0,0.5)')==CMYKColor(1,0,0,0,alpha=0.5)
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   758
    True
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   759
    >>> toColor('pcmyka(100,0,0,0,0.5)')==PCMYKColor(100,0,0,0,alpha=0.5)
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   760
    True
3297
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   761
    '''
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   762
    if isinstance(arg,Color): return arg
3176
c15bdeba0afc colors.py: allow unicode arguments in toColor and elsewhere
rgbecker
parents: 3155
diff changeset
   763
    if isinstance(arg,(tuple,list)):
1723
ad3abfa75c82 Fixed toColors to be even more accepting
rgbecker
parents: 1683
diff changeset
   764
        assert 3<=len(arg)<=4, 'Can only convert 3 and 4 sequences to color'
ad3abfa75c82 Fixed toColors to be even more accepting
rgbecker
parents: 1683
diff changeset
   765
        assert 0<=min(arg) and max(arg)<=1
ad3abfa75c82 Fixed toColors to be even more accepting
rgbecker
parents: 1683
diff changeset
   766
        return len(arg)==3 and Color(arg[0],arg[1],arg[2]) or CMYKColor(arg[0],arg[1],arg[2],arg[3])
3176
c15bdeba0afc colors.py: allow unicode arguments in toColor and elsewhere
rgbecker
parents: 3155
diff changeset
   767
    elif isinstance(arg,basestring):
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   768
        C = cssParse(arg)
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   769
        if C: return C
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   770
        C = getAllNamedColors()
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
   771
        s = arg.lower()
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3300
diff changeset
   772
        if s in C: return C[s]
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   773
        try:
1723
ad3abfa75c82 Fixed toColors to be even more accepting
rgbecker
parents: 1683
diff changeset
   774
            return toColor(eval(arg))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   775
        except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   776
            pass
65
fb9c613d4c13 Allow 0x in HexColor, added stringToColor, fixed caching in getAllNamedColors
rgbecker
parents: 51
diff changeset
   777
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   778
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   779
        return HexColor(arg)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   780
    except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   781
        if default is None:
2916
c894eda9c168 reportlab: obsoletisms removed
rgbecker
parents: 2719
diff changeset
   782
            raise ValueError('Invalid color value %r' % arg)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   783
        return default
1654
eaf787730479 Added setColors fixed toColor
rgbecker
parents: 1481
diff changeset
   784
2392
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   785
def toColorOrNone(arg,default=None):
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   786
    '''as above but allows None as a legal value'''
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   787
    if arg is None:
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   788
        return None
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   789
    else:
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   790
        return toColor(arg, default)
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   791
1654
eaf787730479 Added setColors fixed toColor
rgbecker
parents: 1481
diff changeset
   792
def setColors(**kw):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   793
    UNDEF = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   794
    progress = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   795
    assigned = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   796
    while kw and progress:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   797
        progress = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   798
        for k, v in kw.items():
3176
c15bdeba0afc colors.py: allow unicode arguments in toColor and elsewhere
rgbecker
parents: 3155
diff changeset
   799
            if isinstance(v,(tuple,list)):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   800
                c = map(lambda x,UNDEF=UNDEF: toColor(x,UNDEF),v)
3176
c15bdeba0afc colors.py: allow unicode arguments in toColor and elsewhere
rgbecker
parents: 3155
diff changeset
   801
                if isinstance(v,tuple): c = tuple(c)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   802
                ok = UNDEF not in c
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   803
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   804
                c = toColor(v,UNDEF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   805
                ok = c is not UNDEF
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   806
            if ok:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   807
                assigned[k] = c
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   808
                del kw[k]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   809
                progress = 1
1654
eaf787730479 Added setColors fixed toColor
rgbecker
parents: 1481
diff changeset
   810
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   811
    if kw: raise ValueError("Can't convert\n%s" % str(kw))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   812
    getAllNamedColors()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   813
    for k, c in assigned.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   814
        globals()[k] = c
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   815
        if isinstance(c,Color): _namedColors[k] = c
1659
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   816
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   817
def Whiter(c,f):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   818
    '''given a color combine with white as c*f w*(1-f) 0<=f<=1'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   819
    c = toColor(c)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   820
    if isinstance(c,PCMYKColor):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   821
        w = _PCMYK_white
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   822
    elif isinstance(c,CMYKColor): w = _CMYK_white
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   823
    else: w = white
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   824
    return linearlyInterpolatedColor(w, c, 0, 1, f)
1659
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   825
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   826
def Blacker(c,f):
2136
7cf4744dd435 Fix doc comment
rgbecker
parents: 2047
diff changeset
   827
    '''given a color combine with black as c*f+b*(1-f) 0<=f<=1'''
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   828
    c = toColor(c)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   829
    if isinstance(c,PCMYKColor):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   830
        b = _PCMYK_black
2032
93e160dfbd29 Fixed a typo in function Blacker.
johnprecedo
parents: 2002
diff changeset
   831
    elif isinstance(c,CMYKColor): b = _CMYK_black
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   832
    else: b = black
1723
ad3abfa75c82 Fixed toColors to be even more accepting
rgbecker
parents: 1683
diff changeset
   833
    return linearlyInterpolatedColor(b, c, 0, 1, f)
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   834
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   835
3237
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   836
def fade(aSpotColor, percentages):
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   837
    """Waters down spot colors and returns a list of new ones
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   838
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   839
    e.g fade(myColor, [100,80,60,40,20]) returns a list of five colors
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   840
    """
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   841
    out = []
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   842
    for percent in percentages:
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   843
        frac = percent * 0.01   #assume they give us numbers from 0 to 100
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   844
        newCyan = frac * aSpotColor.cyan
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   845
        newMagenta = frac * aSpotColor.magenta
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   846
        newYellow = frac * aSpotColor.yellow
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   847
        newBlack = frac * aSpotColor.black
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   848
        newDensity = frac * aSpotColor.density
3254
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   849
        newSpot = CMYKColor(    newCyan, newMagenta, newYellow, newBlack,
3237
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   850
                            spotName = aSpotColor.spotName,
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   851
                            density = newDensity)
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   852
        out.append(newSpot)
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   853
    return out
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   854
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   855
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   856
if __name__ == "__main__":
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   857
    import doctest
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   858
    doctest.testmod()