reportlab/demos/colors/colortest.py
changeset 50 61a65ebe894d
child 494 54257447cfe9
equal deleted inserted replaced
49:f19eb245ce5e 50:61a65ebe894d
       
     1 # colortest.py
       
     2 
       
     3 import reportlab.pdfgen.canvas
       
     4 from reportlab.lib import colors
       
     5 from reportlab.lib.units import inch
       
     6 
       
     7 
       
     8 def run():
       
     9     c = reportlab.pdfgen.canvas.Canvas('colortest.pdf')
       
    10 
       
    11     #do a test of CMYK interspersed with RGB
       
    12 
       
    13     #first do RGB values
       
    14     framePage(c, 'Color Demo - RGB Space and CMYK spaces interspersed' )
       
    15 
       
    16     y = 700
       
    17 
       
    18     c.setFillColorRGB(0,0,0)
       
    19     c.drawString(100, y, 'cyan')
       
    20     c.setFillColorCMYK(1,0,0,0)
       
    21     c.rect(200, y, 300, 30, fill=1)
       
    22     y = y - 40
       
    23 
       
    24     c.setFillColorRGB(0,0,0)
       
    25     c.drawString(100, y, 'red')
       
    26     c.setFillColorRGB(1,0,0)
       
    27     c.rect(200, y, 300, 30, fill=1)
       
    28     y = y - 40
       
    29     
       
    30     c.setFillColorRGB(0,0,0)
       
    31     c.drawString(100, y, 'magenta')
       
    32     c.setFillColorCMYK(0,1,0,0)
       
    33     c.rect(200, y, 300, 30, fill=1)
       
    34     y = y - 40
       
    35 
       
    36     c.setFillColorRGB(0,0,0)
       
    37     c.drawString(100, y, 'green')
       
    38     c.setFillColorRGB(0,1,0)
       
    39     c.rect(200, y, 300, 30, fill=1)
       
    40     y = y - 40
       
    41 
       
    42     c.setFillColorRGB(0,0,0)
       
    43     c.drawString(100, y, 'yellow')
       
    44     c.setFillColorCMYK(0,0,1,0)
       
    45     c.rect(200, y, 300, 30, fill=1)
       
    46     y = y - 40
       
    47     
       
    48     c.setFillColorRGB(0,0,0)
       
    49     c.drawString(100, y, 'blue')
       
    50     c.setFillColorRGB(0,0,1)
       
    51     c.rect(200, y, 300, 30, fill=1)
       
    52     y = y - 40
       
    53         
       
    54     c.setFillColorRGB(0,0,0)
       
    55     c.drawString(100, y, 'black')
       
    56     c.setFillColorCMYK(0,0,0,1)
       
    57     c.rect(200, y, 300, 30, fill=1)
       
    58     y = y - 40
       
    59 
       
    60 
       
    61     c.showPage()
       
    62     
       
    63     #do all named colors
       
    64     framePage(c, 'Color Demo - RGB Space - page %d' % c.getPageNumber())
       
    65 
       
    66     all_colors = reportlab.lib.colors.getAllNamedColors().items()
       
    67     all_colors.sort() # alpha order by name
       
    68     c.setFont('Times-Roman', 12)
       
    69     c.drawString(72,730, 'This shows all the named colors in the HTML standard.')
       
    70     y = 700
       
    71     for (name, color) in all_colors:
       
    72         c.setFillColor(colors.black)
       
    73         c.drawString(100, y, name)
       
    74         c.setFillColor(color)
       
    75         c.rect(200, y-10, 300, 30, fill=1)
       
    76         y = y - 40
       
    77         if y < 100:
       
    78             c.showPage()
       
    79             framePage(c, 'Color Demo - RGB Space - page %d' % c.getPageNumber())
       
    80             y = 700
       
    81 
       
    82 
       
    83 
       
    84             
       
    85     c.save()
       
    86 
       
    87 def framePage(canvas, title):
       
    88     canvas.setFont('Times-BoldItalic',20)
       
    89     canvas.drawString(inch, 10.5 * inch, title)
       
    90                             
       
    91     canvas.setFont('Times-Roman',10)
       
    92     canvas.drawCentredString(4.135 * inch, 0.75 * inch,
       
    93                             'Page %d' % canvas.getPageNumber())
       
    94     
       
    95     #draw a border
       
    96     canvas.setStrokeColorRGB(1,0,0)
       
    97     canvas.setLineWidth(5)
       
    98     canvas.line(0.8 * inch, inch, 0.8 * inch, 10.75 * inch)
       
    99     #reset carefully afterwards
       
   100     canvas.setLineWidth(1)
       
   101     canvas.setStrokeColorRGB(0,0,0)
       
   102 
       
   103 if __name__ == '__main__':
       
   104     run()
       
   105