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