I'm working on extension to randomize color of selected objects, something like "color>randomize" but by specific palette colors. So far I made it somewhat to work but not exactly as I wanted.
What I want to do: lets say I select 10 objects and I want to assign random color to each object from specific palette but excluding color that already was randomly chosen on previous objects in that selection set, that is I want each color to be used out, to be unique. In case of my palette consisting of 5 colors and 10 objects being selected I would want this extension to pick unique colors from that palette for first 5 objects and only then colors could start repeating since there are no more unique colors in that palette.
At the moment I made it to load palette from file, parse it to list and randomly color selected objects, but the problem is that most of the time those colors repeat even if I have palette of 5 colors and 5 objects selected.
P.s. I only know a bit of php and can't understand how to go further about this in python.
My code:
Code: Select all
import coloreffect,random,inkex
class C(coloreffect.ColorEffect):
def __init__(self):
coloreffect.ColorEffect.__init__(self)
self.OptionParser.add_option("--palette",
action="store", type="int",
dest="palette", default=1,
help="list item in palette")
self.OptionParser.add_option("--random",
action="store", type="inkbool",
dest="random", default=False,
help="random color from palette")
self.OptionParser.add_option("--refresh",
action="store", type="inkbool",
dest="refresh", default=False,
help="refresh with new colors")
def colmod(self,r,g,b):
palettes = []
f = open('color_palette.cfg', 'r')
for line in f:
palettes.append(line)
from random import choice
if self.options.random:
palette = choice(palettes)
else:
if self.options.palette < len(palettes):
palette = palettes [self.options.palette]
else:
palette = 'ff0000'
palette = palette.split(',')
color = choice(palette)
return str(color)
c = C()
c.affect()
And palette file basically contains lines with hex colors separated by commas, each line represents a color palette:
Code: Select all
874E3B,B5A694,120303,400A0A,851414
69D2E7,A7DBD8,E0E4CC,F38630,FA6900
ECD078,D95B43,C02942,542437,53777A
FE4365,FC9D9A,F9CDAD,C8C8A9,83AF9B
.inx:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<_name>Randomize palettes</_name>
<id>org.inkscape.color.randomize_palette</id>
<dependency type="executable" location="extensions">coloreffect.py</dependency>
<dependency type="executable" location="extensions">color_palette.py</dependency>
<dependency type="executable" location="extensions">simplestyle.py</dependency>
<param name="palette" type="int" min="0" max="100" _gui-text="Palette">true</param>
<param name="random" type="boolean" _gui-text="Random">true</param>
<param name="refresh" type="boolean" _gui-text="Refresh">true</param>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu _name="Color"/>
</effects-menu>
</effect>
<script>
<command reldir="extensions" interpreter="python">color_palette.py</command>
</script>
</inkscape-extension>