home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / CapturePalette.PspScript < prev    next >
Encoding:
Text File  |  2003-04-22  |  9.6 KB  |  224 lines

  1. from JascApp import *
  2. import JascUtils
  3.  
  4. def ScriptProperties():
  5.     return {
  6.         'Author': 'Joe Fromm',
  7.         'Copyright': 'Copyright (C) 2002-2003, Jasc Software Inc., All Rights Reserved. Permission to create derivate works of this script is granted provided this copyright notice is included',
  8.         'Description': 'Make a pseudo-palette - a series of squares filled with solid colors',
  9.         'Host': 'Paint Shop Pro',
  10.         'Host Version': '8.00',
  11.         'ForceTextEditor': App.Constants.Boolean.true
  12.         }
  13.  
  14.  
  15. def Do(Environment):
  16.     if JascUtils.RequireADoc( Environment ) == App.Constants.Boolean.false:
  17.         return
  18.     
  19.     # all of these formats have palettes, and we need a paletted image.
  20.     PalettedFormats = [ App.Constants.PixelFormat.Index1, App.Constants.PixelFormat.Index4,
  21.                         App.Constants.PixelFormat.Index8, App.Constants.PixelFormat.Grey,
  22.                         App.Constants.PixelFormat.GreyA ]
  23.     
  24.     # is the current image paletted?  If not, give them the option to reduce color depth
  25.     Info = App.Do( Environment, 'ReturnImageInfo' )
  26.  
  27.     if not Info['PixelFormat'] in PalettedFormats:
  28.         result = App.Do(Environment,  'MsgBox', {
  29.                 'Buttons': App.Constants.MsgButtons.YesNo, 
  30.                 'Icon': App.Constants.MsgIcons.Question, 
  31.                 'Text': 'This script requires a paletted image.  Would you like to '
  32.                         'reduce the color depth?', 
  33.                 })
  34.         if result == 0:
  35.             return
  36.         else:
  37.             # always do this interactively, even if the script as a whole is silent
  38.             App.Do( Environment, 'DecreaseColorsToX', {
  39.                         'GeneralSettings': {
  40.                             'ExecutionMode': App.Constants.ExecutionMode.Interactive, 
  41.                             'AutoActionMode': App.Constants.AutoActionMode.Match
  42.                             }
  43.                         })
  44.  
  45.             # get a new copy of the image info since it just changed
  46.             Info = App.Do( Environment, 'ReturnImageInfo' )
  47.  
  48.     ColorList = Info[ 'PaletteColorList' ]
  49.     if not ColorList:
  50.         print 'Internal error:  No palette found'
  51.         return
  52.  
  53.     # now make a pseudo-palette - an image with little swatches of color against a solid
  54.     # background.  Make the background a medium grey, and separate each swatch by 2 pixels
  55.     # of grout.  Each color square is 10x10
  56.     # *** Change these parameters for a different output image
  57.     MakePseudoPalette( Environment, ColorList, GroutColor=(128,128,128), GroutWidth=2,
  58.                        ColorsPerRow=16, TileSize=(10,10), ButtonMargin=0 )
  59.  
  60.     # we just created a new image - just leave it open for the user to decide what to do
  61.  
  62.  
  63.  
  64.  
  65.  
  66. # Create a new image that appears somewhat like a palette - a series of color swatches 
  67. # organized into rows and columns. The resulting image can be customized by setting the
  68. # parameters appropriately.
  69. # ColorList - list of (r,g,b) tuples with the colors to use.  Must be between 2 and 1024
  70. # GroutColor - underlying color of the image, seen between the color tiles.  Defaults to
  71. #              medium grey
  72. # GroutWidth - how much grout is seen between color tiles.  Defaults to 2 pixels.
  73. # ColorsPerRow - number of tiles on each row.  Along with TileSize[0], determines
  74. #                the width of the image. Defaults to 16
  75. # TileSize - (x,y) size tuple.  A rectangle of that size will be filled with each color.
  76. #            along with length of the colorlist and ColorsPerRow, determines the resulting
  77. #            size of the image.  Defaults to (10,10)
  78. # ButtonMargin - how much of a buttonize effect to apply to each color tile.  Default of 0
  79. #                means no buttonize is done.  A buttonize of 2 yields a slight 3D effect.
  80. def MakePseudoPalette( Environment, ColorList, GroutColor=(0,0,0),
  81.                        GroutWidth=2, ColorsPerRow=16, TileSize=(10,10), ButtonMargin=0):
  82.     # lets put some sanity checks on our arguments - these values are arbitrary for the
  83.     # most part, so if you don't like them, change them.
  84.     if not GroutWidth in range(0,20):
  85.         print 'GroutWidth value must be between 0 and 20'
  86.         return 0
  87.     if not ColorsPerRow in range(1,100):
  88.         print 'ColorsPerRow value must be between 1 and 100'
  89.         return 0
  90.     if not TileSize[0] in range(2, 50):
  91.         print 'TileSize must be between 2 and 50'
  92.         return 0
  93.     if not TileSize[1] in range(2, 50):
  94.         print 'TileSize must be between 2 and 50'
  95.         return 0
  96.     if not len(ColorList) in range( 2, 1024 ):
  97.         print 'Number of colors must be between 2 and 1024'
  98.         return 0
  99.     if not ButtonMargin in range( 0, min(TileSize)/2 ):
  100.         print 'ButtonMargin must be less than half the tilesize'
  101.         return 0
  102.     
  103.     # run through the list of colors we were given and remove consecutive duplicates
  104.     # this will address the issue of reducing to an odd number of colors, but permit
  105.     # duplicates to occur in the middle of the color list
  106.     OldColor = ( 1000, 0, 0 )   # not a legal color value, so should never occur in nature
  107.     iStartDup = 0
  108.     iCurrentIndex = 0
  109.     
  110.     for Color in ColorList:
  111.         if Color != OldColor:
  112.             iStartDup = iCurrentIndex
  113.             OldColor = Color
  114.             
  115.         iCurrentIndex += 1
  116.  
  117.     if iStartDup + 1 < len(ColorList):
  118.         ColorList = ColorList[ 0 : iStartDup+1 ]
  119.  
  120.     # start by determining how big a document we want to create
  121.     NumRows = (len(ColorList) + ColorsPerRow - 1) / ColorsPerRow
  122.     Height = (NumRows * TileSize[1]) + ((NumRows + 1) * GroutWidth)
  123.     Width = (ColorsPerRow * TileSize[0]) + ((ColorsPerRow + 1) * GroutWidth)
  124.  
  125.     # make an image of the proper size using a background of the grout color
  126.     App.Do( Environment, 'NewFile', {
  127.             'Width': Width,
  128.             'Height': Height, 
  129.             'ColorDepth': App.Constants.Colordepth.SixteenMillionColor, 
  130.             'DimensionUnits': App.Constants.DimensionType.Pixels, 
  131.             'FillMaterial': {
  132.                 'Color': GroutColor, 
  133.                 'Pattern': None, 
  134.                 'Gradient': None, 
  135.                 'Texture': None
  136.                 }, 
  137.             'Transparent': App.Constants.Boolean.false, 
  138.             'VectorBackground': App.Constants.Boolean.false, 
  139.             'GeneralSettings': {
  140.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  141.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  142.                 }
  143.             })
  144.  
  145.     TargetDoc = App.ActiveDocument
  146.     
  147.     # now iterate across the list of colors
  148.     RowNumber = 0
  149.     ColNumber = 0
  150.     RowStart = GroutWidth
  151.     ColStart = GroutWidth
  152.     Index = 0 
  153.     for Color in ColorList:
  154.         # just send data to the output so they know something is happening.
  155.         print 'Color %d is (%02X,%02X,%02X)' % (Index, Color[0], Color[1], Color[2])
  156.         Index += 1
  157.         
  158.         # when we reach the end of a row reset our column and advance row
  159.         if ColNumber >= ColorsPerRow:
  160.             ColNumber = 0
  161.             RowNumber += 1
  162.             RowStart += TileSize[1] + GroutWidth
  163.             ColStart = GroutWidth
  164.  
  165.         # make a selection of the rectangle this color should cover
  166.         App.Do( Environment, 'Selection', {
  167.                 'General': {
  168.                     'Mode': App.Constants.SelectionOperation.Replace, 
  169.                     'Antialias': App.Constants.Boolean.false, 
  170.                     'Feather': 0
  171.                     }, 
  172.                 'SelectionShape': App.Constants.SelectionShape.Rectangle, 
  173.                 'Start': (ColStart, RowStart), 
  174.                 'End': (ColStart + TileSize[0], RowStart + TileSize[1]), 
  175.                 'GeneralSettings': {
  176.                     'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  177.                     'AutoActionMode': App.Constants.AutoActionMode.Match
  178.                     }
  179.                 }, TargetDoc )
  180.  
  181.         # fill it with the color
  182.         App.Do( Environment, 'Fill', {
  183.                 'BlendMode': 0, 
  184.                 'MatchMode': 1, 
  185.                 'Material': {
  186.                     'Color': Color, 
  187.                     'Pattern': None, 
  188.                     'Gradient': None, 
  189.                     'Texture': None
  190.                     }, 
  191.                 'UseForground': App.Constants.Boolean.true, 
  192.                 'Opacity': 100, 
  193.                 'Point': (ColStart+1, RowStart+1), 
  194.                 'SampleMerged': 0, 
  195.                 'Tolerance': 20, 
  196.                 'GeneralSettings': {
  197.                     'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  198.                     'AutoActionMode': App.Constants.AutoActionMode.Match
  199.                     }
  200.                 }, TargetDoc )
  201.  
  202.         # buttonize the color if requested
  203.         if ButtonMargin != 0:
  204.             App.Do( Environment, 'Buttonize', {
  205.                 'Width': ButtonMargin, 
  206.                 'Height': ButtonMargin, 
  207.                 'Transparent': App.Constants.Boolean.false, 
  208.                 'Color': (255,255,255), 
  209.                 'Opacity': 75, 
  210.                 'GeneralSettings': {
  211.                     'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  212.                     'AutoActionMode': App.Constants.AutoActionMode.Match
  213.                     }
  214.                 }, TargetDoc)
  215.  
  216.         # go on to the next column        
  217.         ColNumber += 1
  218.         ColStart += TileSize[0] + GroutWidth
  219.         
  220.     # select none to get rid of the selection on the last color square.
  221.     App.Do( Environment, 'SelectNone', {}, TargetDoc )
  222.     
  223.     return 1 # success
  224.