#!/usr/bin/python # Copyright (C) 2005 Adrian Likins # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # random_gradient.py: Generates a random palette. # based on palettes_gradients.py from Joao S. O. Bueno Calligaris # # TODO: # The "nearby" colors should be chosen in HSV space instead of RGB, I think the # results will be much better. # # import random import time from gimpfu import * def gauss_color(mean=128, stdev=64): col = int(random.gauss(mean,stdev)) if col > 255: r = 255 return col def rand_color(): # we could theorectially use other dists if we cared return gauss_color() def gauss_opacity(mean=80, stdev=24): op = random.gauss(mean, stdev) if op > 100.0: op = 100 return op def rand_op(): return gauss_opacity() # attempt to sorta stay around the main color, but munge it around a bit def munge_color(color, stdev=12): r = gauss_color(color[0],stdev) g = gauss_color(color[1],stdev) b = gauss_color(color[2],stdev) a = gauss_opacity(color[3],stdev) return (r,g,b,a) def random_gradient(numcolors, model): gradient_name = pdb.gimp_gradient_new("random-gradient-%s" % time.ctime()) last_segment = 0 blending = GRADIENT_SEGMENT_LINEAR colors = [] for i in range(numcolors): colors.append((random.randint(0,255), random.randint(0,255), random.randint(0,255), 100)) # for each really random color, pick a couple sort of random associates to go along with # how many? well, lets make that random shall we MAXNUMDEVS = 2 MAXMUNGE=18 #numdevs = random.randint(0,maxnumdevs) new_colors = [] # iterate over a copy, so we can munge copy in place index = 0 for color in colors[:]: numdevs = random.randint(0,MAXNUMDEVS) count = 0 while (count < numdevs): new_color = munge_color(color, MAXMUNGE) # so we've got a new color, what do we do with it ? #lets put it either before or after the current index # the theory being some related colors around our "seed" colors # or something like that... print index if (random.randint(0,1)): colors.insert(index-1,new_color) else: colors.insert(index+1,new_color) count = count + 1 index = index + 1 number_of_segments = len(colors) print colors #A new gradient has one new segemtn from white to black #for someunknown force of the universe. the call to "gimp...replicate" #only accepts up to 20 copies. for i in xrange (number_of_segments - 2): pdb.gimp_gradient_segment_range_replicate (gradient_name, i, -1, 2) pdb.gimp_gradient_segment_range_redistribute_handles (gradient_name, 0, -1) pdb.gimp_gradient_segment_range_set_blending_function (gradient_name, 0, -1, random.randint(0,4)) pdb.gimp_gradient_segment_range_set_coloring_type (gradient_name, 0, -1, random.randint(0,2)) if len(colors) < 1: return if number_of_segments == 1: pdb.gimp_gradient_segment_set_right_color (gradient_name, 0, colors[0], 100) pdb.gimp_gradient_segment_set_left_color (gradient_name, 0, colors[0], 100) return for i in xrange (number_of_segments - 1): pdb.gimp_gradient_segment_set_left_color (gradient_name, i, colors[i][:3], colors[i][3]) if blending != None: pdb.gimp_gradient_segment_set_right_color (gradient_name, i, colors[i + 1][:3], colors[i+1][3]) else: pdb.gimp_gradient_segment_set_right_color (gradient_name, i, colors[i][:3], colors[i+1][3]) return register( "random_gradient", "create a random gradient", "create a random gradient", "Adrian Likins", "(k) All rites reversed - reuse whatever you like- JS", "2005", "/Xtns/Python-Fu/Gradients/Random", "", [ (PF_INT, "numcolors", "Number of Colors", 10), #change in python fu for something nicer: (PF_RADIO, "model", "Color model to use", "RGB", (("RGB", "RGB"), ("HSV", "HSV"))), ], [], random_gradient) main ()