src/reportlab/lib/colors.py
author rptlab
Tue, 30 Apr 2013 14:28:14 +0100
branchpy33
changeset 3723 99aa837b6703
parent 3721 0c93dd8ff567
child 3781 df8b57380768
permissions -rw-r--r--
second stage of port to Python 3.3; working hello world
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3617
ae5744e97c42 reportlab: copyright date changes
robin
parents: 3540
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2012
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
3496
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    15
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    16
These tests are here because doctest cannot find them otherwise.
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    17
>>> toColor('rgb(128,0,0)')==toColor('rgb(50%,0%,0%)')
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    18
True
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    19
>>> toColor('rgb(50%,0%,0%)')!=Color(0.5,0,0,1)
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    20
True
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    21
>>> toColor('hsl(0,100%,50%)')==toColor('rgb(255,0,0)')
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    22
True
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    23
>>> toColor('hsl(-120,100%,50%)')==toColor('rgb(0,0,255)')
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    24
True
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    25
>>> toColor('hsl(120,100%,50%)')==toColor('rgb(0,255,0)')
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    26
True
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    27
>>> toColor('rgba( 255,0,0,0.5)')==Color(1,0,0,0.5)
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    28
True
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    29
>>> toColor('cmyk(1,0,0,0 )')==CMYKColor(1,0,0,0)
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    30
True
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    31
>>> toColor('pcmyk( 100 , 0 , 0 , 0 )')==PCMYKColor(100,0,0,0)
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    32
True
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    33
>>> toColor('cmyka(1,0,0,0,0.5)')==CMYKColor(1,0,0,0,alpha=0.5)
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    34
True
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    35
>>> toColor('pcmyka(100,0,0,0,0.5)')==PCMYKColor(100,0,0,0,alpha=0.5)
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    36
True
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    37
>>> toColor('pcmyka(100,0,0,0)')
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    38
Traceback (most recent call last):
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    39
    ....
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    40
ValueError: css color 'pcmyka(100,0,0,0)' has wrong number of components
3029
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    41
'''
3496
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
    42
import math, re
1061
11c13c203d02 Made import from utils absolute to avoid bombing Andy's yaml stuff
rgbecker
parents: 1041
diff changeset
    43
from reportlab.lib.utils import fp_str
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
    44
import collections
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
    45
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
    46
class Color:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    47
    """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
    48
    are in the range 0 (dark) to 1 (full intensity)."""
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
    49
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    50
    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
    51
        "Initialize with red, green, blue in range [0-1]."
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    52
        self.red = red
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    53
        self.green = green
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    54
        self.blue = blue
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    55
        self.alpha = alpha
563
4cdf14b5c761 Syncing with pingo
rgbecker
parents: 494
diff changeset
    56
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    57
    def __repr__(self):
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    58
        return "Color(%s)" % fp_str(*(self.red, self.green, self.blue,self.alpha)).replace(' ',',')
563
4cdf14b5c761 Syncing with pingo
rgbecker
parents: 494
diff changeset
    59
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    60
    def __hash__(self):
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    61
        return hash((self.red, self.green, self.blue, self.alpha))
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
    62
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    63
    def __cmp__(self,other):
3300
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    64
        '''simple comparison by component; cmyk != color ever
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    65
        >>> cmp(Color(0,0,0),None)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    66
        -1
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    67
        >>> cmp(Color(0,0,0),black)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    68
        0
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    69
        >>> 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
    70
        (-1, True)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    71
        '''
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    72
        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
    73
        try:
3300
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
    74
            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
    75
                    (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
    76
        except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    77
            return -1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    78
        return 0
66
85e0ca12b6b2 Added ColorType
rgbecker
parents: 65
diff changeset
    79
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    80
    def rgb(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    81
        "Returns a three-tuple of components"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    82
        return (self.red, self.green, self.blue)
1663
1473a2b159d3 Added Color.hexval
rgbecker
parents: 1659
diff changeset
    83
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    84
    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
    85
        "Returns a four-tuple of components"
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
    86
        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
    87
2001
49ed2369f56b Added bitmap_rgb function
rgbecker
parents: 1991
diff changeset
    88
    def bitmap_rgb(self):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
    89
        return tuple([int(x*255)&255 for x in self.rgb()])
2001
49ed2369f56b Added bitmap_rgb function
rgbecker
parents: 1991
diff changeset
    90
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    91
    def bitmap_rgba(self):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
    92
        return tuple([int(x*255)&255 for x in self.rgba()])
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    93
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
    94
    def hexval(self):
2001
49ed2369f56b Added bitmap_rgb function
rgbecker
parents: 1991
diff changeset
    95
        return '0x%02x%02x%02x' % self.bitmap_rgb()
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
    96
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    97
    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
    98
        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
    99
3473
c56be984bc24 colros.py: add methods int_rgb & int_rgba to Color
rgbecker
parents: 3446
diff changeset
   100
    def int_rgb(self):
c56be984bc24 colros.py: add methods int_rgb & int_rgba to Color
rgbecker
parents: 3446
diff changeset
   101
        v = self.bitmap_rgb()
c56be984bc24 colros.py: add methods int_rgb & int_rgba to Color
rgbecker
parents: 3446
diff changeset
   102
        return v[0]<<16|v[1]<<8|v[2]
c56be984bc24 colros.py: add methods int_rgb & int_rgba to Color
rgbecker
parents: 3446
diff changeset
   103
c56be984bc24 colros.py: add methods int_rgb & int_rgba to Color
rgbecker
parents: 3446
diff changeset
   104
    def int_rgba(self):
c56be984bc24 colros.py: add methods int_rgb & int_rgba to Color
rgbecker
parents: 3446
diff changeset
   105
        v = self.bitmap_rgba()
c56be984bc24 colros.py: add methods int_rgb & int_rgba to Color
rgbecker
parents: 3446
diff changeset
   106
        return int((v[0]<<24|v[1]<<16|v[2]<<8|v[3])&0xffffff)
c56be984bc24 colros.py: add methods int_rgb & int_rgba to Color
rgbecker
parents: 3446
diff changeset
   107
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   108
    _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
   109
    def cKwds(self):
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   110
        for k in self._cKwds:
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   111
            yield k,getattr(self,k)
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   112
    cKwds=property(cKwds)
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   113
3155
e7deb49947dd colors.py: add clone method
rgbecker
parents: 3097
diff changeset
   114
    def clone(self,**kwds):
e7deb49947dd colors.py: add clone method
rgbecker
parents: 3097
diff changeset
   115
        '''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
   116
        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
   117
        D.update(kwds)
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   118
        return self.__class__(**D)
3155
e7deb49947dd colors.py: add clone method
rgbecker
parents: 3097
diff changeset
   119
3446
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   120
    def _lookupName(self,D={}):
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   121
        if not D:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
   122
            for n,v in getAllNamedColors().items():
3446
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   123
                if not isinstance(v,CMYKColor):
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   124
                    t = v.red,v.green,v.blue
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   125
                    if t in D:
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   126
                        n = n+'/'+D[t]
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   127
                    D[t] = n
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   128
        t = self.red,self.green,self.blue
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   129
        return t in D and D[t] or None
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   130
3633
427f295dd7ba support geting the normalized alpha
rgbecker
parents: 3617
diff changeset
   131
    @property
427f295dd7ba support geting the normalized alpha
rgbecker
parents: 3617
diff changeset
   132
    def normalizedAlpha(self):
427f295dd7ba support geting the normalized alpha
rgbecker
parents: 3617
diff changeset
   133
        return self.alpha
427f295dd7ba support geting the normalized alpha
rgbecker
parents: 3617
diff changeset
   134
3444
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   135
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
   136
class CMYKColor(Color):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   137
    """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
   138
    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
   139
    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
   140
    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
   141
    to an approximate function.
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
   142
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   143
    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
   144
    the color attributes should not be changed afterwards.
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
   145
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   146
    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
   147
    and renderers may look for these."""
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
   148
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   149
    _scale = 1.0
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   150
    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
   151
                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
   152
        """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   153
        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
   154
        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
   155
        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
   156
        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
   157
        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
   158
        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
   159
        """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   160
        self.cyan = cyan
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   161
        self.magenta = magenta
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   162
        self.yellow = yellow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   163
        self.black = black
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   164
        self.spotName = spotName
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   165
        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
   166
        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
   167
        self.alpha = alpha
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
   168
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   169
        # 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
   170
        self.red, self.green, self.blue = cmyk2rgb( (cyan, magenta, yellow, black) )
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
   171
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   172
        if density<1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   173
            #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
   174
            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
   175
            r = density*(r-1)+1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   176
            g = density*(g-1)+1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   177
            b = density*(b-1)+1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   178
            self.red, self.green, self.blue = (r,g,b)
1041
ca66f62767b5 Generalised CMYKColor added PCMYKColor
rgbecker
parents: 894
diff changeset
   179
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   180
    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
   181
        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
   182
            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
   183
            (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
   184
            (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
   185
            (self.knockout is not None and (',knockout=%d' % self.knockout) or ''),
3444
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   186
            (self.alpha is not None and (',alpha=%s' % self.alpha) or ''),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   187
            )
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
   188
3377
8122289973f5 colors.py: modify fader method
rgbecker
parents: 3375
diff changeset
   189
    def fader(self, n, reverse=False):
8122289973f5 colors.py: modify fader method
rgbecker
parents: 3375
diff changeset
   190
        '''return n colors based on density fade
8122289973f5 colors.py: modify fader method
rgbecker
parents: 3375
diff changeset
   191
        *NB* note this dosen't reach density zero'''
8122289973f5 colors.py: modify fader method
rgbecker
parents: 3375
diff changeset
   192
        scale = self._scale
8122289973f5 colors.py: modify fader method
rgbecker
parents: 3375
diff changeset
   193
        dd = scale/float(n)
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
   194
        L = [self.clone(density=scale - i*dd) for i in range(n)]
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   195
        if reverse: L.reverse()
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   196
        return L
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   197
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   198
    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
   199
        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
   200
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   201
    def __cmp__(self,other):
3300
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   202
        """obvious way to compare colours
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   203
        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
   204
        >>> cmp(CMYKColor(0,0,0,1),None)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   205
        -1
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   206
        >>> cmp(CMYKColor(0,0,0,1),_CMYK_black)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   207
        0
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   208
        >>> cmp(PCMYKColor(0,0,0,100),_CMYK_black)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   209
        0
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   210
        >>> 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
   211
        (-1, True)
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   212
        """
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   213
        if not isinstance(other, CMYKColor): return -1
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   214
        try:
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   215
            return cmp(
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   216
                (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
   217
                (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
   218
        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
   219
            return -1
3300
9906989730e9 colors.py: improve comparison funcs so they actually work
rgbecker
parents: 3297
diff changeset
   220
        return 0
696
735615146ad7 Added CMYK support to core
andy_robinson
parents: 695
diff changeset
   221
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   222
    def cmyk(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   223
        "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
   224
        return (self.cyan, self.magenta, self.yellow, self.black)
695
083c92f7ed2b Initial checkin of CMYKColor class
andy_robinson
parents: 641
diff changeset
   225
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   226
    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
   227
        "Returns a tuple of five color components - syntactic sugar"
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
   228
        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
   229
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   230
    def _density_str(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   231
        return fp_str(self.density)
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   232
    _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
   233
3446
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   234
    def _lookupName(self,D={}):
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   235
        if not D:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
   236
            for n,v in getAllNamedColors().items():
3446
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   237
                if isinstance(v,CMYKColor):
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   238
                    t = v.cyan,v.magenta,v.yellow,v.black
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   239
                    if t in D:
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   240
                        n = n+'/'+D[t]
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   241
                    D[t] = n
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   242
        t = self.cyan,self.magenta,self.yellow,self.black
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   243
        return t in D and D[t] or None
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   244
3633
427f295dd7ba support geting the normalized alpha
rgbecker
parents: 3617
diff changeset
   245
    @property
427f295dd7ba support geting the normalized alpha
rgbecker
parents: 3617
diff changeset
   246
    def normalizedAlpha(self):
427f295dd7ba support geting the normalized alpha
rgbecker
parents: 3617
diff changeset
   247
        return self.alpha*self._scale
427f295dd7ba support geting the normalized alpha
rgbecker
parents: 3617
diff changeset
   248
1041
ca66f62767b5 Generalised CMYKColor added PCMYKColor
rgbecker
parents: 894
diff changeset
   249
class PCMYKColor(CMYKColor):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   250
    '''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
   251
    _scale = 100.
3292
2719247a42cd colors.py: use 0-100 for alpha in PCMYK colors
rgbecker
parents: 3290
diff changeset
   252
    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
   253
        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
   254
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   255
    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
   256
        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
   257
            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
   258
            (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
   259
            (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
   260
            (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
   261
            (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
   262
            )
1041
ca66f62767b5 Generalised CMYKColor added PCMYKColor
rgbecker
parents: 894
diff changeset
   263
3225
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   264
    def cKwds(self):
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   265
        K=self._cKwds
3375
baeae60700c7 colors.py: add CMYK color fading method & fix bug in scaling
rgbecker
parents: 3333
diff changeset
   266
        S=K[:6]
3225
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   267
        for k in self._cKwds:
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   268
            v=getattr(self,k)
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   269
            if k in S: v*=100
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   270
            yield k,v
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   271
    cKwds=property(cKwds)
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   272
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
   273
class CMYKColorSep(CMYKColor):
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
   274
    '''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
   275
    _scale = 1.
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
   276
    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
   277
                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
   278
        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
   279
    _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
   280
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   281
class PCMYKColorSep(PCMYKColor,CMYKColorSep):
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   282
    '''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
   283
    _scale = 100.
3225
b034291925d9 colors.py: attempt to fix up the cloning of various color instances
rgbecker
parents: 3198
diff changeset
   284
    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
   285
                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
   286
        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
   287
    _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
   288
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3300
diff changeset
   289
def cmyk2rgb(cmyk,density=1):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   290
    "Convert from a CMYK color tuple to an RGB color tuple"
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3300
diff changeset
   291
    c,m,y,k = cmyk
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   292
    # 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
   293
    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
   294
    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
   295
    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
   296
    return (r,g,b)
51
6ebbabd7abf1 Sync with pingo
rgbecker
parents: 42
diff changeset
   297
430
f5c0a69cdcd9 added rgb2cmyk
rgbecker
parents: 270
diff changeset
   298
def rgb2cmyk(r,g,b):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   299
    '''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
   300
    c = 1 - r
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   301
    m = 1 - g
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   302
    y = 1 - b
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   303
    k = min(c,m,y)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   304
    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
   305
    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
   306
    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
   307
    k = min(1,max(0,k))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   308
    return (c,m,y,k)
430
f5c0a69cdcd9 added rgb2cmyk
rgbecker
parents: 270
diff changeset
   309
641
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   310
def color2bw(colorRGB):
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   311
    "Transform an RGB color to a black and white equivalent."
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   312
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   313
    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
   314
    r, g, b, a = col.red, col.green, col.blue, col.alpha
641
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   315
    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
   316
    bwColorRGB = Color(n, n, n, a)
641
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   317
    return bwColorRGB
b9432db96ab7 Added color2bw function.
dinu_gherman
parents: 563
diff changeset
   318
3643
e741636f4781 colors.py: add bugfix from Dinu Gherman
rptlab
parents: 3633
diff changeset
   319
def HexColor(val, htmlOnly=False, hasAlpha=False):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   320
    """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
   321
    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
   322
    AA is the red, BB is the green, and CC is the blue (00-FF).
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   323
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   324
    An alpha value can also be given in the form #AABBCCDD or 0xAABBCCDD where
3643
e741636f4781 colors.py: add bugfix from Dinu Gherman
rptlab
parents: 3633
diff changeset
   325
    DD is the alpha value if hasAlpha is True.
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   326
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   327
    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
   328
    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
   329
    only the #aabbcc form is allowed.
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   330
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   331
    >>> HexColor('#ffffff')
3295
66da176a4a4f Fixed colors doctests
damian
parents: 3292
diff changeset
   332
    Color(1,1,1,1)
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   333
    >>> HexColor('#FFFFFF')
3295
66da176a4a4f Fixed colors doctests
damian
parents: 3292
diff changeset
   334
    Color(1,1,1,1)
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   335
    >>> HexColor('0xffffff')
3295
66da176a4a4f Fixed colors doctests
damian
parents: 3292
diff changeset
   336
    Color(1,1,1,1)
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   337
    >>> HexColor('16777215')
3295
66da176a4a4f Fixed colors doctests
damian
parents: 3292
diff changeset
   338
    Color(1,1,1,1)
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   339
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   340
    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
   341
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   342
    >>> HexColor('ffffff')
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   343
    Traceback (most recent call last):
3295
66da176a4a4f Fixed colors doctests
damian
parents: 3292
diff changeset
   344
    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
   345
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   346
    >>> HexColor('#FFFFFF', htmlOnly=True)
3295
66da176a4a4f Fixed colors doctests
damian
parents: 3292
diff changeset
   347
    Color(1,1,1,1)
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   348
    >>> HexColor('0xffffff', htmlOnly=True)
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   349
    Traceback (most recent call last):
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   350
    ValueError: not a hex string
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   351
    >>> HexColor('16777215', htmlOnly=True)
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   352
    Traceback (most recent call last):
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   353
    ValueError: not a hex string
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   354
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   355
    """ #" for emacs
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   356
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
   357
    if isinstance(val,str):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   358
        b = 10
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   359
        if val[:1] == '#':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   360
            val = val[1:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   361
            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
   362
            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
   363
                alpha = True
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   364
        else:
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   365
            if htmlOnly:
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   366
                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
   367
            if val[:2].lower() == '0x':
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   368
                b = 16
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   369
                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
   370
                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
   371
                    alpha = True
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3176
diff changeset
   372
        val = int(val,b)
3643
e741636f4781 colors.py: add bugfix from Dinu Gherman
rptlab
parents: 3633
diff changeset
   373
    if hasAlpha:
e741636f4781 colors.py: add bugfix from Dinu Gherman
rptlab
parents: 3633
diff changeset
   374
        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
   375
    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
   376
1154
c6314e34823b Added method to color for interpolating colors - changed shadedRect in
johnprecedo
parents: 1120
diff changeset
   377
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
   378
    """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   379
    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
   380
    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
   381
    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
   382
    """
1154
c6314e34823b Added method to color for interpolating colors - changed shadedRect in
johnprecedo
parents: 1120
diff changeset
   383
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   384
    if c0.__class__ != c1.__class__:
3410
d300c040b7b7 piecharts.py: remove unwanted if
rgbecker
parents: 3377
diff changeset
   385
        raise ValueError("Color classes must be the same for interpolation!\nGot %r and %r'"%(c0,c1))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   386
    if x1<x0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   387
        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
   388
    if x<x0-1e-8 or x>x1+1e-8: # fudge factor for numerical problems
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
   389
        raise ValueError("Can't interpolate: x=%f is not between %f and %f!" % (x,x0,x1))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   390
    if x<=x0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   391
        return c0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   392
    elif x>=x1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   393
        return c1
1154
c6314e34823b Added method to color for interpolating colors - changed shadedRect in
johnprecedo
parents: 1120
diff changeset
   394
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   395
    cname = c0.__class__.__name__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   396
    dx = float(x1-x0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   397
    x = x-x0
1154
c6314e34823b Added method to color for interpolating colors - changed shadedRect in
johnprecedo
parents: 1120
diff changeset
   398
2047
7ff0f67de98e jython compatibility hack
dragan1
parents: 2032
diff changeset
   399
    if cname == 'Color': # RGB
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   400
        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
   401
        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
   402
        b = c0.blue+x*(c1.blue - c0.blue)/dx
3279
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
   403
        a = c0.alpha+x*(c1.alpha - c0.alpha)/dx
11a5f9aae5c0 reportlab: use alpha=1 as default for colours
rgbecker
parents: 3277
diff changeset
   404
        return Color(r,g,b,alpha=a)
2047
7ff0f67de98e jython compatibility hack
dragan1
parents: 2032
diff changeset
   405
    elif cname == 'CMYKColor':
3411
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   406
        if cmykDistance(c0,c1)<1e-8:
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   407
            #colors same do density and preserve spotName if any
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   408
            assert c0.spotName == c1.spotName, "Identical cmyk, but different spotName"
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   409
            c = c0.cyan
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   410
            m = c0.magenta
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   411
            y = c0.yellow
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   412
            k = c0.black
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   413
            d = c0.density+x*(c1.density - c0.density)/dx
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   414
            a = c0.alpha+x*(c1.alpha - c0.alpha)/dx
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   415
            return CMYKColor(c,m,y,k, density=d, spotName=c0.spotName, alpha=a)
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   416
        elif cmykDistance(c0,_CMYK_white)<1e-8:
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   417
            #special c0 is white
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   418
            c = c1.cyan
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   419
            m = c1.magenta
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   420
            y = c1.yellow
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   421
            k = c1.black
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   422
            d = x*c1.density/dx
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   423
            a = x*c1.alpha/dx
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   424
            return CMYKColor(c,m,y,k, density=d, spotName=c1.spotName, alpha=a)
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   425
        elif cmykDistance(c1,_CMYK_white)<1e-8:
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   426
            #special c1 is white
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   427
            c = c0.cyan
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   428
            m = c0.magenta
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   429
            y = c0.yellow
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   430
            k = c0.black
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   431
            d = x*c0.density/dx
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   432
            d = c0.density*(1-x/dx)
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   433
            a = c0.alpha*(1-x/dx)
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   434
            return PCMYKColor(c,m,y,k, density=d, spotName=c0.spotName, alpha=a)
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   435
        else:
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   436
            c = c0.cyan+x*(c1.cyan - c0.cyan)/dx
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   437
            m = c0.magenta+x*(c1.magenta - c0.magenta)/dx
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   438
            y = c0.yellow+x*(c1.yellow - c0.yellow)/dx
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   439
            k = c0.black+x*(c1.black - c0.black)/dx
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   440
            d = c0.density+x*(c1.density - c0.density)/dx
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   441
            a = c0.alpha+x*(c1.alpha - c0.alpha)/dx
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   442
            return CMYKColor(c,m,y,k, density=d, alpha=a)
2047
7ff0f67de98e jython compatibility hack
dragan1
parents: 2032
diff changeset
   443
    elif cname == 'PCMYKColor':
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   444
        if cmykDistance(c0,c1)<1e-8:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   445
            #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
   446
            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
   447
            c = c0.cyan
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   448
            m = c0.magenta
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   449
            y = c0.yellow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   450
            k = c0.black
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   451
            d = c0.density+x*(c1.density - c0.density)/dx
3411
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   452
            a = c0.alpha+x*(c1.alpha - c0.alpha)/dx
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   453
            return PCMYKColor(c*100,m*100,y*100,k*100, density=d*100,
3411
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   454
                              spotName=c0.spotName, alpha=100*a)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   455
        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
   456
            #special c0 is white
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   457
            c = c1.cyan
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   458
            m = c1.magenta
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   459
            y = c1.yellow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   460
            k = c1.black
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   461
            d = x*c1.density/dx
3411
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   462
            a = x*c1.alpha/dx
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   463
            return PCMYKColor(c*100,m*100,y*100,k*100, density=d*100,
3411
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   464
                              spotName=c1.spotName, alpha=a*100)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   465
        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
   466
            #special c1 is white
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   467
            c = c0.cyan
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   468
            m = c0.magenta
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   469
            y = c0.yellow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   470
            k = c0.black
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   471
            d = x*c0.density/dx
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   472
            d = c0.density*(1-x/dx)
3411
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   473
            a = c0.alpha*(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
   474
            return PCMYKColor(c*100,m*100,y*100,k*100, density=d*100,
3411
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   475
                              spotName=c0.spotName, alpha=a*100)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   476
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   477
            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
   478
            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
   479
            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
   480
            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
   481
            d = c0.density+x*(c1.density - c0.density)/dx
3284
dcb7eb12976d colors.py: fix mis-spelt reference
rgbecker
parents: 3282
diff changeset
   482
            a = c0.alpha+x*(c1.alpha - c0.alpha)/dx
3411
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   483
            return PCMYKColor(c*100,m*100,y*100,k*100, density=d*100, alpha=a*100)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   484
    else:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
   485
        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
   486
3254
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   487
def obj_R_G_B(c):
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   488
    '''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
   489
    if isinstance(c,Color):
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   490
        return c.red,c.green,c.blue
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   491
    elif isinstance(c,(tuple,list)):
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   492
        if len(c)==3:
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   493
            return tuple(c)
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   494
        elif len(c)==4:
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   495
            return toColor(c).rgb()
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   496
        else:
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   497
            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
   498
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   499
# special case -- indicates no drawing should be done
696
735615146ad7 Added CMYK support to core
andy_robinson
parents: 695
diff changeset
   500
# 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
   501
transparent = Color(0,0,0,alpha=0)
563
4cdf14b5c761 Syncing with pingo
rgbecker
parents: 494
diff changeset
   502
1308
da76452fcd92 Forgot _CMYK_white again
rgbecker
parents: 1307
diff changeset
   503
_CMYK_white=CMYKColor(0,0,0,0)
1659
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   504
_PCMYK_white=PCMYKColor(0,0,0,0)
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   505
_CMYK_black=CMYKColor(0,0,0,1)
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   506
_PCMYK_black=PCMYKColor(0,0,0,100)
1307
73a24d4d6aba Forgot _CMYK_white
rgbecker
parents: 1306
diff changeset
   507
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   508
# Special colors
2154
03798f09cb1f Fix to use latest blue
rgbecker
parents: 2136
diff changeset
   509
ReportLabBlueOLD = HexColor(0x4e5688)
03798f09cb1f Fix to use latest blue
rgbecker
parents: 2136
diff changeset
   510
ReportLabBlue = HexColor(0x00337f)
03798f09cb1f Fix to use latest blue
rgbecker
parents: 2136
diff changeset
   511
ReportLabBluePCMYK = PCMYKColor(100,65,0,30,spotName='Pantone 288U')
1429
6fd172dc8d64 Added ReportLabLightBlue.
dinu_gherman
parents: 1310
diff changeset
   512
ReportLabLightBlue = HexColor(0xb7b9d3)
1844
9ad0785bb912 Added special reportLabFid colours
rgbecker
parents: 1727
diff changeset
   513
ReportLabFidBlue=HexColor(0x3366cc)
9ad0785bb912 Added special reportLabFid colours
rgbecker
parents: 1727
diff changeset
   514
ReportLabFidRed=HexColor(0xcc0033)
1991
1c4729336cb6 added ReportLabGreen
fuzzypuffin
parents: 1844
diff changeset
   515
ReportLabGreen = HexColor(0x336600)
1c4729336cb6 added ReportLabGreen
fuzzypuffin
parents: 1844
diff changeset
   516
ReportLabLightGreen = HexColor(0x339933)
894
096027d2aa04 Added special color - ReportLabBlue.
johnprecedo
parents: 696
diff changeset
   517
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   518
# 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
   519
aliceblue =     HexColor(0xF0F8FF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   520
antiquewhite =  HexColor(0xFAEBD7)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   521
aqua =  HexColor(0x00FFFF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   522
aquamarine =    HexColor(0x7FFFD4)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   523
azure =     HexColor(0xF0FFFF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   524
beige =     HexColor(0xF5F5DC)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   525
bisque =    HexColor(0xFFE4C4)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   526
black =     HexColor(0x000000)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   527
blanchedalmond =    HexColor(0xFFEBCD)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   528
blue =  HexColor(0x0000FF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   529
blueviolet =    HexColor(0x8A2BE2)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   530
brown =     HexColor(0xA52A2A)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   531
burlywood =     HexColor(0xDEB887)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   532
cadetblue =     HexColor(0x5F9EA0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   533
chartreuse =    HexColor(0x7FFF00)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   534
chocolate =     HexColor(0xD2691E)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   535
coral =     HexColor(0xFF7F50)
1727
5125562d1372 Add cornflowerblue = cornflower
rgbecker
parents: 1723
diff changeset
   536
cornflowerblue = cornflower =   HexColor(0x6495ED)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   537
cornsilk =  HexColor(0xFFF8DC)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   538
crimson =   HexColor(0xDC143C)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   539
cyan =  HexColor(0x00FFFF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   540
darkblue =  HexColor(0x00008B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   541
darkcyan =  HexColor(0x008B8B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   542
darkgoldenrod =     HexColor(0xB8860B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   543
darkgray =  HexColor(0xA9A9A9)
3097
9c90e1e4baaa colors.py: normalize spelling of grey/gray colours, suggested by Paul Barass
rgbecker
parents: 3029
diff changeset
   544
darkgrey =  darkgray
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   545
darkgreen =     HexColor(0x006400)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   546
darkkhaki =     HexColor(0xBDB76B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   547
darkmagenta =   HexColor(0x8B008B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   548
darkolivegreen =    HexColor(0x556B2F)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   549
darkorange =    HexColor(0xFF8C00)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   550
darkorchid =    HexColor(0x9932CC)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   551
darkred =   HexColor(0x8B0000)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   552
darksalmon =    HexColor(0xE9967A)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   553
darkseagreen =  HexColor(0x8FBC8B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   554
darkslateblue =     HexColor(0x483D8B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   555
darkslategray =     HexColor(0x2F4F4F)
3097
9c90e1e4baaa colors.py: normalize spelling of grey/gray colours, suggested by Paul Barass
rgbecker
parents: 3029
diff changeset
   556
darkslategrey = darkslategray
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   557
darkturquoise =     HexColor(0x00CED1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   558
darkviolet =    HexColor(0x9400D3)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   559
deeppink =  HexColor(0xFF1493)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   560
deepskyblue =   HexColor(0x00BFFF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   561
dimgray =   HexColor(0x696969)
3097
9c90e1e4baaa colors.py: normalize spelling of grey/gray colours, suggested by Paul Barass
rgbecker
parents: 3029
diff changeset
   562
dimgrey = dimgray
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   563
dodgerblue =    HexColor(0x1E90FF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   564
firebrick =     HexColor(0xB22222)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   565
floralwhite =   HexColor(0xFFFAF0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   566
forestgreen =   HexColor(0x228B22)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   567
fuchsia =   HexColor(0xFF00FF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   568
gainsboro =     HexColor(0xDCDCDC)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   569
ghostwhite =    HexColor(0xF8F8FF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   570
gold =  HexColor(0xFFD700)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   571
goldenrod =     HexColor(0xDAA520)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   572
gray =  HexColor(0x808080)
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   573
grey = gray
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   574
green =     HexColor(0x008000)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   575
greenyellow =   HexColor(0xADFF2F)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   576
honeydew =  HexColor(0xF0FFF0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   577
hotpink =   HexColor(0xFF69B4)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   578
indianred =     HexColor(0xCD5C5C)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   579
indigo =    HexColor(0x4B0082)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   580
ivory =     HexColor(0xFFFFF0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   581
khaki =     HexColor(0xF0E68C)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   582
lavender =  HexColor(0xE6E6FA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   583
lavenderblush =     HexColor(0xFFF0F5)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   584
lawngreen =     HexColor(0x7CFC00)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   585
lemonchiffon =  HexColor(0xFFFACD)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   586
lightblue =     HexColor(0xADD8E6)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   587
lightcoral =    HexColor(0xF08080)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   588
lightcyan =     HexColor(0xE0FFFF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   589
lightgoldenrodyellow =  HexColor(0xFAFAD2)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   590
lightgreen =    HexColor(0x90EE90)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   591
lightgrey =     HexColor(0xD3D3D3)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   592
lightpink =     HexColor(0xFFB6C1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   593
lightsalmon =   HexColor(0xFFA07A)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   594
lightseagreen =     HexColor(0x20B2AA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   595
lightskyblue =  HexColor(0x87CEFA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   596
lightslategray =    HexColor(0x778899)
3097
9c90e1e4baaa colors.py: normalize spelling of grey/gray colours, suggested by Paul Barass
rgbecker
parents: 3029
diff changeset
   597
lightslategrey = lightslategray
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   598
lightsteelblue =    HexColor(0xB0C4DE)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   599
lightyellow =   HexColor(0xFFFFE0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   600
lime =  HexColor(0x00FF00)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   601
limegreen =     HexColor(0x32CD32)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   602
linen =     HexColor(0xFAF0E6)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   603
magenta =   HexColor(0xFF00FF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   604
maroon =    HexColor(0x800000)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   605
mediumaquamarine =  HexColor(0x66CDAA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   606
mediumblue =    HexColor(0x0000CD)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   607
mediumorchid =  HexColor(0xBA55D3)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   608
mediumpurple =  HexColor(0x9370DB)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   609
mediumseagreen =    HexColor(0x3CB371)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   610
mediumslateblue =   HexColor(0x7B68EE)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   611
mediumspringgreen =     HexColor(0x00FA9A)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   612
mediumturquoise =   HexColor(0x48D1CC)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   613
mediumvioletred =   HexColor(0xC71585)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   614
midnightblue =  HexColor(0x191970)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   615
mintcream =     HexColor(0xF5FFFA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   616
mistyrose =     HexColor(0xFFE4E1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   617
moccasin =  HexColor(0xFFE4B5)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   618
navajowhite =   HexColor(0xFFDEAD)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   619
navy =  HexColor(0x000080)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   620
oldlace =   HexColor(0xFDF5E6)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   621
olive =     HexColor(0x808000)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   622
olivedrab =     HexColor(0x6B8E23)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   623
orange =    HexColor(0xFFA500)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   624
orangered =     HexColor(0xFF4500)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   625
orchid =    HexColor(0xDA70D6)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   626
palegoldenrod =     HexColor(0xEEE8AA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   627
palegreen =     HexColor(0x98FB98)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   628
paleturquoise =     HexColor(0xAFEEEE)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   629
palevioletred =     HexColor(0xDB7093)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   630
papayawhip =    HexColor(0xFFEFD5)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   631
peachpuff =     HexColor(0xFFDAB9)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   632
peru =  HexColor(0xCD853F)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   633
pink =  HexColor(0xFFC0CB)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   634
plum =  HexColor(0xDDA0DD)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   635
powderblue =    HexColor(0xB0E0E6)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   636
purple =    HexColor(0x800080)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   637
red =   HexColor(0xFF0000)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   638
rosybrown =     HexColor(0xBC8F8F)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   639
royalblue =     HexColor(0x4169E1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   640
saddlebrown =   HexColor(0x8B4513)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   641
salmon =    HexColor(0xFA8072)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   642
sandybrown =    HexColor(0xF4A460)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   643
seagreen =  HexColor(0x2E8B57)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   644
seashell =  HexColor(0xFFF5EE)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   645
sienna =    HexColor(0xA0522D)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   646
silver =    HexColor(0xC0C0C0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   647
skyblue =   HexColor(0x87CEEB)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   648
slateblue =     HexColor(0x6A5ACD)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   649
slategray =     HexColor(0x708090)
3097
9c90e1e4baaa colors.py: normalize spelling of grey/gray colours, suggested by Paul Barass
rgbecker
parents: 3029
diff changeset
   650
slategrey = slategray
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   651
snow =  HexColor(0xFFFAFA)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   652
springgreen =   HexColor(0x00FF7F)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   653
steelblue =     HexColor(0x4682B4)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   654
tan =   HexColor(0xD2B48C)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   655
teal =  HexColor(0x008080)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   656
thistle =   HexColor(0xD8BFD8)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   657
tomato =    HexColor(0xFF6347)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   658
turquoise =     HexColor(0x40E0D0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   659
violet =    HexColor(0xEE82EE)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   660
wheat =     HexColor(0xF5DEB3)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   661
white =     HexColor(0xFFFFFF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   662
whitesmoke =    HexColor(0xF5F5F5)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   663
yellow =    HexColor(0xFFFF00)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   664
yellowgreen =   HexColor(0x9ACD32)
1654
eaf787730479 Added setColors fixed toColor
rgbecker
parents: 1481
diff changeset
   665
fidblue=HexColor(0x3366cc)
2490
49f8090be0f8 colors.py: add a fidred
rgbecker
parents: 2402
diff changeset
   666
fidred=HexColor(0xcc0033)
2402
da8c6a14c84f minor changes for pdf canvas compatibility and tests
rgbecker
parents: 2392
diff changeset
   667
fidlightblue=HexColor("#d6e0f5")
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   668
66
85e0ca12b6b2 Added ColorType
rgbecker
parents: 65
diff changeset
   669
ColorType=type(black)
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   670
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   671
    ################################################################
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   672
    #
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   673
    #  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
   674
    #  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
   675
    #  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
   676
    #
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   677
    #################################################################
563
4cdf14b5c761 Syncing with pingo
rgbecker
parents: 494
diff changeset
   678
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   679
def colorDistance(col1, col2):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   680
    """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
   681
    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
   682
    names for things."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   683
    return math.sqrt(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   684
            (col1.red - col2.red)**2 +
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   685
            (col1.green - col2.green)**2 +
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   686
            (col1.blue - col2.blue)**2
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   687
            )
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   688
1306
cd0441305584 Improved interpolation added cmykDistance
rgbecker
parents: 1221
diff changeset
   689
def cmykDistance(col1, col2):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   690
    """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
   691
    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
   692
    names for things."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   693
    return math.sqrt(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   694
            (col1.cyan - col2.cyan)**2 +
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   695
            (col1.magenta - col2.magenta)**2 +
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   696
            (col1.yellow - col2.yellow)**2 +
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   697
            (col1.black - col2.black)**2
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   698
            )
1306
cd0441305584 Improved interpolation added cmykDistance
rgbecker
parents: 1221
diff changeset
   699
67
a2da44ccdb34 Fixed getAllNamedColors, needed global etc
rgbecker
parents: 66
diff changeset
   700
_namedColors = None
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   701
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   702
def getAllNamedColors():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   703
    #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
   704
    # uses a singleton for efficiency
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   705
    global _namedColors
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   706
    if _namedColors is not None: return _namedColors
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
   707
    from . import colors
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   708
    _namedColors = {}
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   709
    for name, value in colors.__dict__.items():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   710
        if isinstance(value, Color):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   711
            _namedColors[name] = value
65
fb9c613d4c13 Allow 0x in HexColor, added stringToColor, fixed caching in getAllNamedColors
rgbecker
parents: 51
diff changeset
   712
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   713
    return _namedColors
37
9a37924fd7a7 Initial Checkin
andy_robinson
parents:
diff changeset
   714
461
228b00fb140a Fix for sun compiler
rgbecker
parents: 431
diff changeset
   715
def describe(aColor,mode=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   716
    '''finds nearest colour match to aColor.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   717
    mode=0 print a string desription
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   718
    mode=1 return a string description
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   719
    mode=2 return (distance, colorName)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   720
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   721
    namedColors = getAllNamedColors()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   722
    closest = (10, None, None)  #big number, name, color
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   723
    for name, color in namedColors.items():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   724
        distance = colorDistance(aColor, color)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   725
        if distance < closest[0]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   726
            closest = (distance, name, color)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   727
    if mode<=1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   728
        s = 'best match is %s, distance %0.4f' % (closest[1], closest[0])
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
   729
        if mode==0: print(s)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   730
        else: return s
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   731
    elif mode==2:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   732
        return (closest[1], closest[0])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   733
    else:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
   734
        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
   735
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   736
def hue2rgb(m1, m2, h):
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   737
    if h<0: h += 1
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   738
    if h>1: h -= 1
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   739
    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
   740
    if h*2<1: return m2
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   741
    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
   742
    return m1
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   743
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   744
def hsl2rgb(h, s, l): 
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   745
    if l<=0.5:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   746
        m2 = l*(s+1)
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   747
    else:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   748
        m2 = l+s-l*s
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   749
    m1 = l*2-m2
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   750
    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
   751
3496
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
   752
import re
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
   753
_re_css = re.compile(r'^\s*(pcmyk|cmyk|rgb|hsl)(a|)\s*\(\s*([^)]*)\)\s*$')
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   754
class cssParse:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   755
    def pcVal(self,v):
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   756
        v = v.strip()
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   757
        try:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   758
            c=eval(v[:-1])
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   759
            if not isinstance(c,(float,int)): raise ValueError
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   760
            c=min(100,max(0,c))/100.
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   761
        except:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   762
            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
   763
        return c
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   764
3297
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   765
    def rgbPcVal(self,v):
22145ebf1cf4 colors.py: fix rgbVal and use rgbPcVal for colors (ie discrete mapped colours)
rgbecker
parents: 3296
diff changeset
   766
        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
   767
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   768
    def rgbVal(self,v):
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   769
        v = v.strip()
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   770
        try:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   771
            c=eval(v[:])
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   772
            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
   773
            return int(min(255,max(0,c)))/255.
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   774
        except:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   775
            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
   776
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   777
    def hueVal(self,v):
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   778
        v = v.strip()
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   779
        try:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   780
            c=eval(v[:])
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   781
            if not isinstance(c,(int,float)): raise ValueError
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   782
            return ((c%360+360)%360)/360.
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   783
        except:
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   784
            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
   785
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   786
    def alphaVal(self,v,c=1,n='alpha'):
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   787
        try:
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   788
            a = eval(v.strip())
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   789
            if not isinstance(a,(int,float)): raise ValueError
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   790
            return min(c,max(0,a))
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   791
        except:
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   792
            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
   793
3496
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
   794
    _n_c = dict(pcmyk=(4,100,True,False),cmyk=(4,1,True,False),hsl=(3,1,False,True),rgb=(3,1,False,False))
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
   795
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   796
    def __call__(self,s):
3496
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
   797
        n = _re_css.match(s)
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
   798
        if not n: return
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   799
        self.s = s
3496
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
   800
        b,c,cmyk,hsl = self._n_c[n.group(1)]
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
   801
        ha = n.group(2)
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
   802
        n = n.group(3).split(',')   #strip parens and split on comma
97191b541cc7 colors.py: fix up cssParse so we can have colors named cmyk_black
rgbecker
parents: 3473
diff changeset
   803
        if len(n)!=(b+(ha and 1 or 0)):
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   804
            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
   805
        if ha:
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   806
            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
   807
        else:
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   808
            a = c
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   809
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   810
        if cmyk:
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   811
            C = self.alphaVal(n[0],c,'cyan')
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   812
            M = self.alphaVal(n[1],c,'magenta')
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   813
            Y = self.alphaVal(n[2],c,'yellow')
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   814
            K = self.alphaVal(n[3],c,'black')
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   815
            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
   816
        else:
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   817
            if hsl:
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   818
                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
   819
            else:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
   820
                R,G,B = list(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
   821
3333
5f8d60b7e5de colors.py: add (p)cmyk(a) handling to cssParse/toColor
rgbecker
parents: 3326
diff changeset
   822
            return Color(R,G,B,a)
3296
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   823
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   824
cssParse=cssParse()
eaa7f20b612e colors.py: attempt at some kind of css parser
rgbecker
parents: 3295
diff changeset
   825
3441
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   826
class toColor:
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   827
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   828
    def __init__(self):
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   829
        self.extraColorsNS = {} #used for overriding/adding to existing color names
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   830
                                #make case insensitive if that's your wish
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   831
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   832
    def setExtraColorsNameSpace(self,NS):
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   833
        self.extraColorsNS = NS
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   834
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   835
    def __call__(self,arg,default=None):
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   836
        '''try to map an arbitrary arg to a color instance
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   837
        '''
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   838
        if isinstance(arg,Color): return arg
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   839
        if isinstance(arg,(tuple,list)):
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   840
            assert 3<=len(arg)<=4, 'Can only convert 3 and 4 sequences to color'
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   841
            assert 0<=min(arg) and max(arg)<=1
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   842
            return len(arg)==3 and Color(arg[0],arg[1],arg[2]) or CMYKColor(arg[0],arg[1],arg[2],arg[3])
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
   843
        elif isinstance(arg,str):
3441
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   844
            C = cssParse(arg)
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   845
            if C: return C
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   846
            if arg in self.extraColorsNS: return self.extraColorsNS[arg]
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   847
            C = getAllNamedColors()
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   848
            s = arg.lower()
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   849
            if s in C: return C[s]
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   850
            try:
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   851
                return toColor(eval(arg))
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   852
            except:
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   853
                pass
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   854
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   855
        try:
3441
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   856
            return HexColor(arg)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   857
        except:
3441
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   858
            if default is None:
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   859
                raise ValueError('Invalid color value %r' % arg)
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   860
            return default
65
fb9c613d4c13 Allow 0x in HexColor, added stringToColor, fixed caching in getAllNamedColors
rgbecker
parents: 51
diff changeset
   861
3441
574d72e1ea74 colors.py: allow specified pre-namespacse for toColor
rgbecker
parents: 3411
diff changeset
   862
toColor = toColor()
1654
eaf787730479 Added setColors fixed toColor
rgbecker
parents: 1481
diff changeset
   863
2392
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   864
def toColorOrNone(arg,default=None):
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   865
    '''as above but allows None as a legal value'''
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   866
    if arg is None:
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   867
        return None
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   868
    else:
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   869
        return toColor(arg, default)
6c59b0eb312d table color cycle changes
andy
parents: 2332
diff changeset
   870
1654
eaf787730479 Added setColors fixed toColor
rgbecker
parents: 1481
diff changeset
   871
def setColors(**kw):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   872
    UNDEF = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   873
    progress = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   874
    assigned = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   875
    while kw and progress:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   876
        progress = 0
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   877
        for k, v in kw.items():
3176
c15bdeba0afc colors.py: allow unicode arguments in toColor and elsewhere
rgbecker
parents: 3155
diff changeset
   878
            if isinstance(v,(tuple,list)):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
   879
                c = list(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
   880
                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
   881
                ok = UNDEF not in c
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   882
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   883
                c = toColor(v,UNDEF)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   884
                ok = c is not UNDEF
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   885
            if ok:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   886
                assigned[k] = c
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   887
                del kw[k]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   888
                progress = 1
1654
eaf787730479 Added setColors fixed toColor
rgbecker
parents: 1481
diff changeset
   889
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   890
    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
   891
    getAllNamedColors()
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   892
    for k, c in assigned.items():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   893
        globals()[k] = c
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   894
        if isinstance(c,Color): _namedColors[k] = c
1659
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   895
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   896
def Whiter(c,f):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   897
    '''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
   898
    c = toColor(c)
3411
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   899
    if isinstance(c,CMYKColorSep):
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   900
        c = c.clone()
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   901
        if isinstance(c,PCMYKColorSep):
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   902
            c.__class__ = PCMYKColor
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   903
        else:
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   904
            c.__class__ = CMYKColor
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   905
    if isinstance(c,PCMYKColor):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   906
        w = _PCMYK_white
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   907
    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
   908
    else: w = white
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   909
    return linearlyInterpolatedColor(w, c, 0, 1, f)
1659
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   910
0711987437b6 Added Whiter, Blacker and improved toColor
rgbecker
parents: 1654
diff changeset
   911
def Blacker(c,f):
2136
7cf4744dd435 Fix doc comment
rgbecker
parents: 2047
diff changeset
   912
    '''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
   913
    c = toColor(c)
3411
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   914
    if isinstance(c,CMYKColorSep):
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   915
        c = c.clone()
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   916
        if isinstance(c,PCMYKColorSep):
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   917
            c.__class__ = PCMYKColor
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   918
        else:
dc9b7ac34b8f colors.py: attempt to fix linear interpolation for CMYKColorSep class
rgbecker
parents: 3410
diff changeset
   919
            c.__class__ = CMYKColor
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   920
    if isinstance(c,PCMYKColor):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1663
diff changeset
   921
        b = _PCMYK_black
2032
93e160dfbd29 Fixed a typo in function Blacker.
johnprecedo
parents: 2002
diff changeset
   922
    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
   923
    else: b = black
1723
ad3abfa75c82 Fixed toColors to be even more accepting
rgbecker
parents: 1683
diff changeset
   924
    return linearlyInterpolatedColor(b, c, 0, 1, f)
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
   925
3237
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   926
def fade(aSpotColor, percentages):
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   927
    """Waters down spot colors and returns a list of new ones
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   928
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   929
    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
   930
    """
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   931
    out = []
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   932
    for percent in percentages:
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   933
        frac = percent * 0.01   #assume they give us numbers from 0 to 100
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   934
        newCyan = frac * aSpotColor.cyan
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   935
        newMagenta = frac * aSpotColor.magenta
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   936
        newYellow = frac * aSpotColor.yellow
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   937
        newBlack = frac * aSpotColor.black
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   938
        newDensity = frac * aSpotColor.density
3254
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3237
diff changeset
   939
        newSpot = CMYKColor(    newCyan, newMagenta, newYellow, newBlack,
3237
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   940
                            spotName = aSpotColor.spotName,
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   941
                            density = newDensity)
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   942
        out.append(newSpot)
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   943
    return out
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
   944
3446
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   945
def _enforceError(kind,c,tc):
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   946
    if isinstance(tc,Color):
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   947
        xtra = tc._lookupName()
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   948
        xtra = xtra and '(%s)'%xtra or ''
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   949
    else:
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   950
        xtra = ''
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   951
    raise ValueError('Non %s color %r%s' % (kind,c,xtra))
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   952
3444
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   953
def _enforceSEP(c):
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   954
    '''pure separating colors only, this makes black a problem'''
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   955
    tc = toColor(c)
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   956
    if not isinstance(tc,CMYKColorSep):
3446
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   957
        _enforceError('separating',c,tc)
3444
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   958
    return tc
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   959
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   960
def _enforceSEP_BLACK(c):
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   961
    '''separating + blacks only'''
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   962
    tc = toColor(c)
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   963
    if not isinstance(tc,CMYKColorSep):
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   964
        if isinstance(tc,Color) and tc.red==tc.blue==tc.green: #ahahahah it's a grey
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   965
            tc = _CMYK_black.clone(density=1-tc.red)
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   966
        elif not (isinstance(tc,CMYKColor) and tc.cyan==tc.magenta==tc.yellow==0): #ie some shade of grey
3446
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   967
            _enforceError('separating or black',c,tc)
3444
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   968
    return tc
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   969
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   970
def _enforceSEP_CMYK(c):
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   971
    '''separating or cmyk only'''
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   972
    tc = toColor(c)
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   973
    if not isinstance(tc,CMYKColorSep):
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   974
        if isinstance(tc,Color) and tc.red==tc.blue==tc.green: #ahahahah it's a grey
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   975
            tc = _CMYK_black.clone(density=1-tc.red)
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   976
        elif not isinstance(tc,CMYKColor):
3446
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   977
            _enforceError('separating or CMYK',c,tc)
3444
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   978
    return tc
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   979
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   980
def _enforceCMYK(c):
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   981
    '''cmyk outputs only (rgb greys converted)'''
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   982
    tc = toColor(c)
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   983
    if not isinstance(tc,CMYKColor):
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   984
        if isinstance(tc,Color) and tc.red==tc.blue==tc.green: #ahahahah it's a grey
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   985
            tc = _CMYK_black.clone(black=1-tc.red,alpha=tc.alpha)
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   986
        else:
3446
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   987
            _enforceError('CMYK',c,tc)
3444
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   988
    elif isinstance(tc,CMYKColorSep):
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   989
        tc = tc.clone()
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   990
        tc.__class__ = CMYKColor
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   991
    return tc
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   992
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   993
def _enforceRGB(c):
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   994
    tc = toColor(c)
3446
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   995
    if isinstance(tc,CMYKColor):
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   996
        if tc.cyan==tc.magenta==tc.yellow==0: #ahahahah it's grey
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   997
            v = 1-tc.black*tc.density
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
   998
            tc = Color(v,v,v,alpha=tc.alpha)
3444
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
   999
        else:
3446
7efc421d924a colors.py: fixes to _enforceColorSpace stuff
rgbecker
parents: 3444
diff changeset
  1000
            _enforceError('RGB',c,tc)
3444
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1001
    return tc
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1002
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1003
def _chooseEnforceColorSpace(enforceColorSpace):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
  1004
    if enforceColorSpace is not None and not isinstance(enforceColorSpace, collections.Callable):
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3643
diff changeset
  1005
        if isinstance(enforceColorSpace,str): enforceColorSpace=enforceColorSpace.upper()
3444
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1006
        if enforceColorSpace=='CMYK':
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1007
            enforceColorSpace = _enforceCMYK
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1008
        elif enforceColorSpace=='RGB':
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1009
            enforceColorSpace = _enforceRGB
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1010
        elif enforceColorSpace=='SEP':
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1011
            enforceColorSpace = _enforceSEP
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1012
        elif enforceColorSpace=='SEP_BLACK':
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1013
            enforceColorSpace = _enforceSEP_BLACK
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1014
        elif enforceColorSpace=='SEP_CMYK':
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1015
            enforceColorSpace = _enforceSEP_CMYK
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1016
        else:
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1017
            raise ValueError('Invalid value for Canvas argument enforceColorSpace=%r' % enforceColorSpace)
bea839deb0c1 reportlab: initial checkin for colorspace handling
rgbecker
parents: 3441
diff changeset
  1018
    return enforceColorSpace
3237
8149dc407054 fade colors function added
meitham
parents: 3225
diff changeset
  1019
2719
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
  1020
if __name__ == "__main__":
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
  1021
    import doctest
2fe0642f3951 Add some tests for, and an extra argument to, HexColor
jjlee
parents: 2490
diff changeset
  1022
    doctest.testmod()