src/reportlab/lib/colors.py
changeset 3375 baeae60700c7
parent 3333 5f8d60b7e5de
child 3377 8122289973f5
equal deleted inserted replaced
3374:348f9bcb4c11 3375:baeae60700c7
    93     the color attributes should not be changed afterwards.
    93     the color attributes should not be changed afterwards.
    94 
    94 
    95     Extra attributes may be attached to the class to support specific ink models,
    95     Extra attributes may be attached to the class to support specific ink models,
    96     and renderers may look for these."""
    96     and renderers may look for these."""
    97 
    97 
       
    98     _scale = 1.0
    98     def __init__(self, cyan=0, magenta=0, yellow=0, black=0,
    99     def __init__(self, cyan=0, magenta=0, yellow=0, black=0,
    99                 spotName=None, density=1, knockout=None, alpha=1):
   100                 spotName=None, density=1, knockout=None, alpha=1):
   100         """
   101         """
   101         Initialize with four colors in range [0-1]. the optional
   102         Initialize with four colors in range [0-1]. the optional
   102         spotName, density & knockout may be of use to specific renderers.
   103         spotName, density & knockout may be of use to specific renderers.
   131             (self.spotName and (',spotName='+repr(self.spotName)) or ''),
   132             (self.spotName and (',spotName='+repr(self.spotName)) or ''),
   132             (self.density!=1 and (',density='+fp_str(self.density)) or ''),
   133             (self.density!=1 and (',density='+fp_str(self.density)) or ''),
   133             (self.knockout is not None and (',knockout=%d' % self.knockout) or ''),
   134             (self.knockout is not None and (',knockout=%d' % self.knockout) or ''),
   134             (self.alpha is not None and (',alpha=%d' % self.alpha) or ''),
   135             (self.alpha is not None and (',alpha=%d' % self.alpha) or ''),
   135             )
   136             )
       
   137 
       
   138     def fader(self,n, reverse=False):
       
   139         '''return n colors based on density fade'''
       
   140         dd = self._scale/(n-1)
       
   141         L = [self.clone(density=i*dd) for i in xrange(n)]
       
   142         if reverse: L.reverse()
       
   143         return L
   136 
   144 
   137     def __hash__(self):
   145     def __hash__(self):
   138         return hash( (self.cyan, self.magenta, self.yellow, self.black, self.density, self.spotName, self.alpha) )
   146         return hash( (self.cyan, self.magenta, self.yellow, self.black, self.density, self.spotName, self.alpha) )
   139 
   147 
   140     def __cmp__(self,other):
   148     def __cmp__(self,other):
   166         "Returns a tuple of five color components - syntactic sugar"
   174         "Returns a tuple of five color components - syntactic sugar"
   167         return (self.cyan, self.magenta, self.yellow, self.black, self.alpha)
   175         return (self.cyan, self.magenta, self.yellow, self.black, self.alpha)
   168 
   176 
   169     def _density_str(self):
   177     def _density_str(self):
   170         return fp_str(self.density)
   178         return fp_str(self.density)
   171 
   179     _cKwds='cyan magenta yellow black density alpha spotName knockout'.split()
   172     _cKwds='cyan magenta yellow black density spotName knockout alpha'.split()
       
   173 
   180 
   174 class PCMYKColor(CMYKColor):
   181 class PCMYKColor(CMYKColor):
   175     '''100 based CMYKColor with density and a spotName; just like Rimas uses'''
   182     '''100 based CMYKColor with density and a spotName; just like Rimas uses'''
       
   183     _scale = 100.
   176     def __init__(self,cyan,magenta,yellow,black,density=100,spotName=None,knockout=None,alpha=100):
   184     def __init__(self,cyan,magenta,yellow,black,density=100,spotName=None,knockout=None,alpha=100):
   177         CMYKColor.__init__(self,cyan/100.,magenta/100.,yellow/100.,black/100.,spotName,density/100.,knockout=knockout,alpha=alpha/100.)
   185         CMYKColor.__init__(self,cyan/100.,magenta/100.,yellow/100.,black/100.,spotName,density/100.,knockout=knockout,alpha=alpha/100.)
   178 
   186 
   179     def __repr__(self):
   187     def __repr__(self):
   180         return "%s(%s%s%s%s%s)" % (self.__class__.__name__,
   188         return "%s(%s%s%s%s%s)" % (self.__class__.__name__,
   181             fp_str(self.cyan*100, self.magenta*100, self.yellow*100, self.black*100).replace(' ',','),
   189             fp_str(self.cyan*100, self.magenta*100, self.yellow*100, self.black*100).replace(' ',','),
   182             (self.spotName and (',spotName='+repr(self.spotName)) or ''),
   190             (self.spotName and (',spotName='+repr(self.spotName)) or ''),
   183             (self.density!=1 and (',density='+fp_str(self.density*100)) or ''),
   191             (self.density!=1 and (',density='+fp_str(self.density*100)) or ''),
   184             (self.knockout is not None and (',knockout=%d' % self.knockout) or ''),
   192             (self.knockout is not None and (',knockout=%d' % self.knockout) or ''),
   185             (self.alpha is not None and (',alpha=%d' % (self.alpha*100)) or ''),
   193             (self.alpha is not None and (',alpha=%s' % (fp_str(self.alpha*100))) or ''),
   186             )
   194             )
   187 
   195 
   188     def cKwds(self):
   196     def cKwds(self):
   189         K=self._cKwds
   197         K=self._cKwds
   190         S=K[:5]
   198         S=K[:6]
   191         for k in self._cKwds:
   199         for k in self._cKwds:
   192             v=getattr(self,k)
   200             v=getattr(self,k)
   193             if k in S: v*=100
   201             if k in S: v*=100
   194             yield k,v
   202             yield k,v
   195     cKwds=property(cKwds)
   203     cKwds=property(cKwds)
   196 
   204 
   197 class CMYKColorSep(CMYKColor):
   205 class CMYKColorSep(CMYKColor):
   198     '''special case color for making separating pdfs'''
   206     '''special case color for making separating pdfs'''
       
   207     _scale = 1.
   199     def __init__(self, cyan=0, magenta=0, yellow=0, black=0,
   208     def __init__(self, cyan=0, magenta=0, yellow=0, black=0,
   200                 spotName=None, density=1,alpha=1):
   209                 spotName=None, density=1,alpha=1):
   201         CMYKColor.__init__(self,cyan,magenta,yellow,black,spotName,density,knockout=None,alpha=alpha)
   210         CMYKColor.__init__(self,cyan,magenta,yellow,black,spotName,density,knockout=None,alpha=alpha)
   202     _cKwds='cyan magenta yellow black density spotName alpha'.split()
   211     _cKwds='cyan magenta yellow black density alpha spotName'.split()
   203 
   212 
   204 class PCMYKColorSep(PCMYKColor,CMYKColorSep):
   213 class PCMYKColorSep(PCMYKColor,CMYKColorSep):
   205     '''special case color for making separating pdfs'''
   214     '''special case color for making separating pdfs'''
       
   215     _scale = 100.
   206     def __init__(self, cyan=0, magenta=0, yellow=0, black=0,
   216     def __init__(self, cyan=0, magenta=0, yellow=0, black=0,
   207                 spotName=None, density=100, alpha=100):
   217                 spotName=None, density=100, alpha=100):
   208         PCMYKColor.__init__(self,cyan,magenta,yellow,black,density,spotName,knockout=None,alpha=alpha)
   218         PCMYKColor.__init__(self,cyan,magenta,yellow,black,density,spotName,knockout=None,alpha=alpha)
   209     _cKwds='cyan magenta yellow black density spotName alpha'.split()
   219     _cKwds='cyan magenta yellow black density alpha spotName'.split()
   210 
   220 
   211 def cmyk2rgb(cmyk,density=1):
   221 def cmyk2rgb(cmyk,density=1):
   212     "Convert from a CMYK color tuple to an RGB color tuple"
   222     "Convert from a CMYK color tuple to an RGB color tuple"
   213     c,m,y,k = cmyk
   223     c,m,y,k = cmyk
   214     # From the Adobe Postscript Ref. Manual 2nd ed.
   224     # From the Adobe Postscript Ref. Manual 2nd ed.