~danieljabailey/inkscape/arc_node_editor

« back to all changes in this revision

Viewing changes to share/extensions/color_randomize.py

  • Committer: tavmjong-free
  • Date: 2016-05-08 07:44:05 UTC
  • mfrom: (14873.1.1 gtk3)
  • Revision ID: tavmjong@free.fr-20160508074405-rm6tiapoq1ugamph
Start of GTK3 external style sheet support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
 
 
3
 
import random
4
 
 
5
 
import coloreffect
6
 
import inkex
 
2
import coloreffect,random,inkex
7
3
 
8
4
class C(coloreffect.ColorEffect):
9
5
    def __init__(self):
10
6
        coloreffect.ColorEffect.__init__(self)
 
7
        self.OptionParser.add_option("-x", "--hue",
 
8
            action="store", type="inkbool", 
 
9
            dest="hue", default=True,
 
10
            help="Randomize hue")
11
11
        self.OptionParser.add_option("-y", "--hue_range",
12
12
            action="store", type="int", 
13
13
            dest="hue_range", default=0,
14
14
            help="Hue range")
 
15
        self.OptionParser.add_option("-s", "--saturation",
 
16
            action="store", type="inkbool", 
 
17
            dest="saturation", default=True,
 
18
            help="Randomize saturation")
15
19
        self.OptionParser.add_option("-t", "--saturation_range",
16
20
            action="store", type="int", 
17
21
            dest="saturation_range", default=0,
18
22
            help="Saturation range")
 
23
        self.OptionParser.add_option("-l", "--lightness",
 
24
            action="store", type="inkbool", 
 
25
            dest="lightness", default=True,
 
26
            help="Randomize lightness")
19
27
        self.OptionParser.add_option("-m", "--lightness_range",
20
28
            action="store", type="int", 
21
29
            dest="lightness_range", default=0,
22
30
            help="Lightness range")
23
 
        self.OptionParser.add_option("-o", "--opacity_range",
24
 
            action="store", type="int", 
25
 
            dest="opacity_range", default=0,
26
 
            help="Opacity range")
27
31
        self.OptionParser.add_option("--tab",
28
32
            action="store", type="string",
29
33
            dest="tab",
30
34
            help="The selected UI-tab when OK was pressed")
31
35
 
32
 
    def randomize_hsl(self, limit, current_value):
33
 
        limit = 255.0 * limit / 100.0
34
 
        limit /= 2
35
 
        max = int((current_value * 255.0) + limit)
36
 
        min = int((current_value * 255.0) - limit)
37
 
        if max > 255:
38
 
            min = min - (max - 255)
39
 
            max = 255
40
 
        if min < 0:
41
 
            max = max - min
42
 
            min = 0
43
 
        return random.randrange(min, max) / 255.0
44
 
 
45
36
    def colmod(self,r,g,b):
46
37
        hsl = self.rgb_to_hsl(r/255.0, g/255.0, b/255.0)
47
 
        if self.options.hue_range > 0:
48
 
            hsl[0] = self.randomize_hsl(self.options.hue_range, hsl[0])
49
 
        if self.options.saturation_range > 0:
50
 
            hsl[1] = self.randomize_hsl(self.options.saturation_range, hsl[1])
51
 
        if self.options.lightness_range > 0:
52
 
            hsl[2] = self.randomize_hsl(self.options.lightness_range, hsl[2])
 
38
        
 
39
        if(self.options.hue):
 
40
            limit = 255.0 * (1 + self.options.hue_range) / 100.0
 
41
            limit /= 2
 
42
            max = int((hsl[0] * 255.0) + limit)
 
43
            min = int((hsl[0] * 255.0) - limit)
 
44
            if max > 255:
 
45
                min = min - (max - 255)
 
46
                max = 255
 
47
            if min < 0:
 
48
                max = max - min
 
49
                min = 0
 
50
            hsl[0] = random.randrange(min, max)
 
51
            hsl[0] /= 255.0
 
52
        if(self.options.saturation):
 
53
            limit = 255.0 * (1 + self.options.saturation_range) / 100.0
 
54
            limit /= 2
 
55
            max = int((hsl[1] * 255.0) + limit)
 
56
            min = int((hsl[1] * 255.0) - limit)
 
57
            if max > 255:
 
58
                min = min - (max - 255)
 
59
                max = 255
 
60
            if min < 0:
 
61
                max = max - min
 
62
                min = 0
 
63
            hsl[1] = random.randrange(min, max)
 
64
            hsl[1] /= 255.0
 
65
        if(self.options.lightness):
 
66
            limit = 255.0 * (1 + self.options.lightness_range) / 100.0
 
67
            limit /= 2
 
68
            max = int((hsl[2] * 255.0) + limit)
 
69
            min = int((hsl[2] * 255.0) - limit)
 
70
            if max > 255:
 
71
                min = min - (max - 255)
 
72
                max = 255
 
73
            if min < 0:
 
74
                max = max - min
 
75
                min = 0
 
76
            hsl[2] = random.randrange(min, max)
 
77
            hsl[2] /= 255.0
53
78
        rgb = self.hsl_to_rgb(hsl[0], hsl[1], hsl[2])
54
79
        return '%02x%02x%02x' % (rgb[0]*255, rgb[1]*255, rgb[2]*255)
55
80
 
56
 
    def opacmod(self, opacity):
57
 
        if self.options.opacity_range > 0:
58
 
            # maybe not necessary, but better not change things that shouldn't change
59
 
            try: 
60
 
                opacity = float(opacity)
61
 
            except ValueError:
62
 
                return opacity
63
 
 
64
 
            limit = self.options.opacity_range
65
 
            limit /= 2
66
 
            max = opacity*100 + limit
67
 
            min = opacity*100 - limit
68
 
            if max > 100:
69
 
                min = min - (max - 100)
70
 
                max = 100
71
 
            if min < 0:
72
 
                max = max - min
73
 
                min = 0
74
 
            ret = str(random.uniform(min,max)/100)
75
 
            return ret
76
 
        return opacity
77
 
 
78
 
 
79
 
if __name__ == '__main__':
80
 
    c = C()
81
 
    c.affect()
 
81
c = C()
 
82
c.affect()
82
83
 
83
84
 
84
85
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99