home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2135 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.4 KB  |  96 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from math import pi, log, sin, sqrt
  5. import string
  6. EPSILON = 1e-10
  7.  
  8. def linear(middle, pos):
  9.     if pos <= middle:
  10.         if middle < EPSILON:
  11.             return 0
  12.         return 0.5 * pos / middle
  13.     pos <= middle
  14.     pos = pos - middle
  15.     middle = 1 - middle
  16.     if middle < EPSILON:
  17.         return 1
  18.     return 0.5 + 0.5 * pos / middle
  19.  
  20.  
  21. def curved(middle, pos):
  22.     return pos ** (log(0.5) / log(max(middle, EPSILON)))
  23.  
  24.  
  25. def sine(middle, pos):
  26.     return (sin(-pi / 2 + pi * linear(middle, pos)) + 1) / 2
  27.  
  28.  
  29. def sphere_increasing(middle, pos):
  30.     return sqrt(1 - (linear(middle, pos) - 1) ** 2)
  31.  
  32.  
  33. def sphere_decreasing(middle, pos):
  34.     return 1 - sqrt(1 - linear(middle, pos) ** 2)
  35.  
  36. SEGMENTS = [
  37.     linear,
  38.     curved,
  39.     sine,
  40.     sphere_increasing,
  41.     sphere_decreasing]
  42.  
  43. class GradientFile:
  44.     gradient = None
  45.     
  46.     def getpalette(self, entries = 256):
  47.         palette = []
  48.         ix = 0
  49.         (x0, x1, xm, rgb0, rgb1, segment) = self.gradient[ix]
  50.         for i in range(entries):
  51.             x = i / float(entries - 1)
  52.             while x1 < x:
  53.                 ix = ix + 1
  54.                 (x0, x1, xm, rgb0, rgb1, segment) = self.gradient[ix]
  55.             w = x1 - x0
  56.             if w < EPSILON:
  57.                 scale = segment(0.5, 0.5)
  58.             else:
  59.                 scale = segment((xm - x0) / w, (x - x0) / w)
  60.             r = chr(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5))
  61.             g = chr(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5))
  62.             b = chr(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5))
  63.             a = chr(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5))
  64.             palette.append(r + g + b + a)
  65.         
  66.         return (string.join(palette, ''), 'RGBA')
  67.  
  68.  
  69.  
  70. class GimpGradientFile(GradientFile):
  71.     
  72.     def __init__(self, fp):
  73.         if fp.readline()[:13] != 'GIMP Gradient':
  74.             raise SyntaxError, 'not a GIMP gradient file'
  75.         fp.readline()[:13] != 'GIMP Gradient'
  76.         count = int(fp.readline())
  77.         gradient = []
  78.         for i in range(count):
  79.             s = string.split(fp.readline())
  80.             w = map(float, s[:11])
  81.             x0 = w[0]
  82.             x1 = w[2]
  83.             xm = w[1]
  84.             rgb0 = w[3:7]
  85.             rgb1 = w[7:11]
  86.             segment = SEGMENTS[int(s[11])]
  87.             cspace = int(s[12])
  88.             if cspace != 0:
  89.                 raise IOError, 'cannot handle HSV colour space'
  90.             cspace != 0
  91.             gradient.append((x0, x1, xm, rgb0, rgb1, segment))
  92.         
  93.         self.gradient = gradient
  94.  
  95.  
  96.