Better Inkscape Randomise

Other sites and resources for Inkscape users.
2045
Posts: 3
Joined: Mon Jul 27, 2015 10:28 am

Better Inkscape Randomise

Postby 2045 » Wed Jul 29, 2015 4:52 am

I posted this on the Programming Board a while ago and it was suggested I post this here. It's pretty much the normal randomise plugin but with values so you can create more subtle changes.

color_better_randomize.inx:

Code: Select all

    <?xml version="1.0" encoding="UTF-8"?>
    <inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
        <_name>Better Randomize</_name>
        <id>org.inkscape.color.better_randomize</id>
        <dependency type="executable" location="extensions">coloreffect.py</dependency>
        <dependency type="executable" location="extensions">color_better_randomize.py</dependency>
        <dependency type="executable" location="extensions">simplestyle.py</dependency>
        <param name="hueAmount" type="int" min="0" max="100" _gui-text="Hue Amount (%)">50</param>
        <param name="saturationAmount" type="int" min="0" max="100" _gui-text="Saturation Amount (%)">50</param>
        <param name="lightnessAmount" type="int" min="0" max="100" _gui-text="Lightness Amount (%)">50</param>
        <effect>
            <object-type>all</object-type>
            <effects-menu>
                <submenu _name="Color"/>
            </effects-menu>
        </effect>
        <script>
            <command reldir="extensions" interpreter="python">color_better_randomize.py</command>
        </script>
    </inkscape-extension>


color_better_randomize.py:

Code: Select all

    #!/usr/bin/env python

    import coloreffect,random,inkex

    class C(coloreffect.ColorEffect):
        def __init__(self):
            coloreffect.ColorEffect.__init__(self)
            self.OptionParser.add_option("--ha", "--hueAmount",
                action="store", type="int",
                dest="hueAmount", default=50,
                help="Randomize hue amount")
            self.OptionParser.add_option("--sa", "--saturationAmount",
                action="store", type="int",
                dest="saturationAmount", default=50,
                help="Randomize saturation amount")
            self.OptionParser.add_option("--la", "--lightnessAmount",
                action="store", type="int",
                dest="lightnessAmount", default=50,
                help="Randomize lightness amount")

        def colmod(self,r,g,b):
            hsl = self.rgb_to_hsl(r/255.0, g/255.0, b/255.0)
            #hue
            newhsl = (random.random()*2.0)-1.0               #generate number between -1.0 & 1.0
            newhsl = newhsl*((self.options.hueAmount)/100.0) #multiply the value by the slider value
            newhsl = hsl[0] + newhsl                         #add the new value to the object's old value
            newhsl = newhsl % 1                              #make number between 0.0 & 1.0, wrapping round
            hsl[0] = newhsl
            #saturation
            newhsl = (random.random()*2.0)-1.0                      #generate number between -1.0 and 1.0
            newhsl = newhsl*((self.options.saturationAmount)/100.0) #multiply the value by the slider value
            newhsl = hsl[1] + newhsl                                #add the new value to the old value
            newhsl = max(min(newhsl,1.0), 0.0)                      #ensure the number lies between 0.0 and 1.0
            hsl[1] = newhsl
            #lightness
            newhsl = (random.random()*2.0)-1.0                      #same as above
            newhsl = newhsl*((self.options.lightnessAmount)/100.0)
            newhsl = hsl[2] + newhsl
            newhsl = max(min(newhsl,1.0), 0.0)
            hsl[2] = newhsl
            rgb = self.hsl_to_rgb(hsl[0], hsl[1], hsl[2])
            return '%02x%02x%02x' % (rgb[0]*255, rgb[1]*255, rgb[2]*255)

    c = C()
    c.affect()

Return to “Inkscape Resources”