Better Inkscape Randomise

Discussion about writing code for Inkscape.
2045
Posts: 3
Joined: Mon Jul 27, 2015 10:28 am

Better Inkscape Randomise

Postby 2045 » Mon Jul 27, 2015 10:45 am

Hello. I slightly modified the color_randomize plugin so the colour can be varied by amounts rather than just entirely or not at all. I thought it might be helpful for someone, assuming it works properly. I have arrogantly called it Better Randomize.

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()

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: Better Inkscape Randomise

Postby brynn » Mon Jul 27, 2015 4:52 pm

Thank you for your work to make Inkscape better!

Are you asking for advice about its programming? Or is it definitely finished and you want comments from people who use it?

If you're ready for it to be used with Inkscape, you should post it in the Inkscape Resources board, and also get it posted here http://wiki.inkscape.org/wiki/index.php ... Repository. You can get permission to edit the wiki by asking for it on the development mailing list https://inkscape.org/en/community/mailing-lists/

Or you could maybe get it posted here: https://inkscape.org/en/download/addons/ Although I'm not sure how to do that. Maybe ask about it on the same dev mailing list.

Or if you have a website where you plan to offer downloads, some extension makers do that too. I'm trying to compile a list all the extensions floating around the internet, but it's really random chance when I hear about one. In any case, if you do that, would you give me the address, for my list?

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: Better Inkscape Randomise

Postby ~suv » Mon Jul 27, 2015 5:07 pm

You could also consider filing a report in the bug tracker, and attach your enhanced version there - depending on review and testing, it might get merged with the existing 'Color > Randomize' extension (and thus be included in a future release of Inkscape).

2045
Posts: 3
Joined: Mon Jul 27, 2015 10:28 am

Re: Better Inkscape Randomise

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

It's finished, but I'd accept any comments on where it can be improved. I'll post it on Resources and try to get it on the bug tracker. Thanks.


Return to “Programming”