summaryrefslogtreecommitdiff
path: root/lib/Pb/Gradient/__init__.py
blob: a05612b7e8918dfc6cfc56bffd575a2ddc336a71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/python2.7
import re
import time
from subprocess import call 
import simplejson as json
import sys
import os
import sha
from config import *
from Pb import Pb

_default_color_1 = "white"
_default_color_2 = "black"

_default_bevel_percent = "12";

_halftone_values = {
    "checkeredfade": "h6x6a",
    "etchedtransition": "o8x8",
    "bendaydots": "h16x16o",
    "smallerdots1": "h8x8o",
    "smallerdots2": "c7x7w",
    "flatstripes": "o2x2",
}

class PbGradient(Pb):
    example_params = {
      "width" : "200",
      "color1" : "#ffdead",
      "color2" : "blue",
      "stripes" : "true",
      "stripenumber" : "20",
      "gradienttype" : "radial",
      "stripeintensity" : "20",
      "halftone" : "checkeredfade",
      "percentbeveled" : "30",
      "flip" : "true",
      "bevel" : "flatinner",
      "rotate" : "20",
      "height" : "200",
      "filetype" : "jpg",
      "username" : "whatever"
    }
    def __init__(self, **kwargs):
        super(PbGradient,self).__init__(**kwargs);
        _definitions = {
          'width': { 'type':'int', 'default': DEFAULT_WIDTH },
          'height': { 'type':'int', 'default' : DEFAULT_HEIGHT },
          'color1': { 'type':'color', 'default': _default_color_1 },
          'color2': { 'type':'color', 'default': _default_color_2 },
          'stripes': { 'type':'bool' },
          'stripenumber': { 'type':'int', 'default': 0 },
          'stripeintensity': { 'type':'int', 'default': 0 },
          'blurriness': { 'type':'int', 'default': 0 },
          'contrast': { 'type':'int', 'default': 100 },
          'brightness': { 'type':'int', 'default': 100 },
          'saturation': { 'type':'int', 'default': 100 },
          'hue': { 'type':'int', 'default': 100 },
          'halftone': { 'type':'enum', 'enum_values' : [
            'checkeredfade', 'etchedtransition', 'bendaydots',
            'smallerdots1', 'smallerdots2', 'flatstripes',
          ] },
          'bevel': { 'type':'enum', 'enum_values' : [
            'flatout', 'flatinner', 'evenlyframed', 'biginner',
            'bigouter', 'dramaticflatout', 'dramaticflatinner',
          ] },
          'percentbeveled': { 'type':'int', 'default': _default_bevel_percent },
          'tilt': { 'type':'int' },
          'rotate': { 'type':'int' },
          'flip': { 'type':'bool' },
          'flop': { 'type':'bool' },
          'filetype': { 'type':'enum', 'enum_values' : OUTPUT_IMAGE_TYPES, 'default': DEFAULT_FINALFORMAT },
          'gradienttype': { 'type':'enum', 'enum_values' : [
            'default', 'canvas', 'radial',  'colorspace',  
            'mirrored',  'plasmawash',  'gradientwash',  'noise'
          ], 'default': 'default' },
          'username': { 'type':'string' }
        }
        self.params.definitions_import(_definitions, kwargs, classname=self.__class__.__name__);

        self.filename, self.filepath = self._filename_filepath_create()

    def _filename_create(self, **kwargs):         
        _base = "{}{}-{}_{}".format(
           self.__class__.__name__, 
           str(self.params.color1).replace('#','').replace('(','-').replace(')','-'),
           str(self.params.color2).replace('#','').replace('(','-').replace(')','-'),
           self._now,                                         
        )
        if self.params.username: _base += "_%s" % self.params.username
        return _base + ".%s" % self.params.filetype


    def _build_cmd(self):
        cmd = [BIN_CONVERT]
        cmd.extend([
            '-size',
            "{}x{}".format(self.params.width,self.params.height)
        ])
        
        if self.params.rotate: cmd.extend(["-rotate", self.params.rotate])         
        if self.params.tilt: cmd.extend(["-distort","SRT",self.params.tilt])
        if self.params.flip == "true": cmd.append("-flip")
        if self.params.flop == "true": cmd.append("-flop")
        if self.params.contrast: cmd.extend(["-contrast-stretch", self.params.contrast])
        _gradients = {
            "default" : "gradient:{}-{}".format(self.params.color1, self.params.color2),
            "canvas" : ["canvas:{}".format(self.params.color1)],
            "radial" : [
                "radial-gradient:{}-{}".format( self.params.color1, self.params.color2)
                ],
            "colorspace" : [
                "-colorspace",
                "Gray",
                "plasma:{}-{}".format(self.params.color1, self.params.color2)
            ],
            "mirrored" : [
                "plasma:{}-{}".format(self.params.color1, self.params.color2),
                 "\(","+clone","-flop","\)",
                 "append"
            ],
            "plasmawash" : [
                "plasma:{}-{}".format(self.params.color1, self.params.color2),
                "-set","colorspace","HSB"
            ],
            "gradientwash" : [
                "gradient:{}-{}".format(self.params.color1, self.params.color2),
                "-set","colorspace","HSB"
            ],
            "noise" : ["xc:","+noise","Random","-virtual-pixel","tile"]
            }
        cmd += _gradients[str(self.params.gradienttype)]
            
        if self.params.blurriness: 
            cmd.extend(["-blur","0x{}".format(self.params.blurriness),"-auto-level"])
        
        if self.params.stripes == "true" and len(self.params.stripenumber):
            cmd.extend(["-function","Sinusoid"])
            if self.params.stripeintensity:  
                cmd.append("{},{}".format(self.params.stripenumber,self.params.stripeintensity))
            else:
                cmd.append(self.params.stripenumber)
        if str(self.params.halftone) in _halftone_values: 
            cmd.extend([
                "-ordered-dither", 
                _halftone_values[str(self.params.halftone)]
            ])
        cmd += [ 
            '-modulate', 
            "{},{},{}".format(
            self.params.brightness or "100",
            self.params.saturation or "100",
            self.params.hue or "100")
        ]
        cmd.append(self.filepath);
        self._call_cmd(cmd)
        if self.params.bevel: self._make_bevel()

    def _get_bevelvalue(self):
        w, h = map(int, (self.params.width, self.params.height))
        if h >= w:
            bevpercentval = str(int(self.params.percentbeveled)*0.005*int(h))
        else:
            bevpercentval = str(int(self.params.percentbeveled)*0.005*int(w))
        return {
            "flatout": ["-s",bevpercentval,"-m","outer"],
            "flatinner": ["-s",bevpercentval,"-m","inner"],
            "evenlyframed": ["-s ",bevpercentval,"-m", "split"],
            "biginner": ["-s",bevpercentval,"-m","outer","-c","50","-b","red","-a","25"],
            "bigouter": ["-s",bevpercentval,"-m","split","-c","50","-b","red","-a","25"],
            "dramaticflatout": ["-s",bevpercentval,"-m","outer","-a","25","-b","blue"],
            "dramaticflatinner": ["-s",bevpercentval,"-m","outer","-a","25","-b","blue"],
        }[str(self.params.bevel)]

    def _make_bevel(self):
        cmd = [BEVELBORDER]
        cmd += self._get_bevelvalue() 
        cmd += [ self.filepath, self.filepath]
        self._call_cmd(cmd)

    def create(self):
        self._build_cmd()
        self._file_dimensions()
        self._file_size_get()