Skip to content

Instantly share code, notes, and snippets.

@roberto-arista
Created February 25, 2015 09:15
Show Gist options
  • Save roberto-arista/6b0dc3a7b46a874d56dd to your computer and use it in GitHub Desktop.
Save roberto-arista/6b0dc3a7b46a874d56dd to your computer and use it in GitHub Desktop.
Vanilla color manager for Robofont font editor
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#################
# Color manager #
#################
### Modules
from vanilla import Window, Group, ColorWell, TextBox, HorizontalLine
from AppKit import NSColor
from OURTmisc import readColorsFromTSV
### Instructions
class ColorManager(object):
# plugin vars
colors_dict = readColorsFromTSV()
colors_order = [item for item in colors_dict.keys()]
colors_order.sort()
# UI vars
plugin_width = 400
plugin_height = 400
lft_margin = 15
rgt_margin = 15
top_margin = 15
btm_margin = 15
col_margin = 10
net_width = plugin_width - lft_margin - rgt_margin
net_height = plugin_height - top_margin - btm_margin
row_height = 30
colors_y = 0
def __init__(self):
# init window
self.main_window = Window((self.plugin_width, self.plugin_height), "Color Manager")
# colors group
self.main_window.colorsGroup = Group((self.lft_margin, self.top_margin, self.net_width, self.net_height))
# iterating over colors dictionary
for each_item in self.colors_order:
# rgba
red = self.colors_dict[each_item][0]
green = self.colors_dict[each_item][1]
blue = self.colors_dict[each_item][2]
alpha = self.colors_dict[each_item][3]
# declaring a row group
rowGroup = Group((0, self.colors_y, self.net_width, self.row_height))
# color caption
colorName = TextBox((0, self.colors_y + 5, self.net_width * .1, self.row_height), each_item)
setattr(rowGroup, 'colorName%s' % (each_item), colorName)
# color wheel
colorWheel = ColorWell((self.net_width * .1 + self.col_margin, self.colors_y, self.net_width * .35, self.row_height),
callback = self.colorWellEdit,
color = NSColor.colorWithCalibratedRed_green_blue_alpha_(red, green, blue, alpha))
setattr(rowGroup, 'colorWheel%s' % (each_item), colorWheel)
# colors values
colorValues = TextBox((self.net_width * .5 + self.col_margin, self.colors_y + 5, self.net_width * .5, self.row_height),
"R:%.0f, G:%.0f, B:%.0f, A:%.0f" % (red*255, green*255, blue*255, alpha*255))
setattr(rowGroup, 'colorValues%s' % (each_item), colorValues)
# append the group attribute
setattr(self.main_window.colorsGroup, 'color%s' % (each_item), rowGroup)
# next row!
self.colors_y += self.row_height
# closing window
self.main_window.open()
def colorWellEdit(self, sender):
print sender.get()
if __name__ == '__main__':
ColorManager()
@typemytype
Copy link

a fix

#!/usr/bin/env python
# -*- coding: utf-8 -*-

#################
# Color manager #
#################

### Modules
from vanilla import Window, Group, ColorWell, TextBox, HorizontalLine
from AppKit import NSColor
#from OURTmisc import readColorsFromTSV


### Instructions
class ColorManager(object):

    # plugin vars
    colors_dict = {"red" : (1, 0, 0, 1), "blue": (0, 0, 1, 1)}#readColorsFromTSV()
    colors_order = [item for item in colors_dict.keys()]
    colors_order.sort()

    # UI vars
    plugin_width  = 400
    plugin_height = 400

    lft_margin = 15
    rgt_margin = 15
    top_margin = 15
    btm_margin = 15
    col_margin = 10

    net_width = plugin_width - lft_margin - rgt_margin
    net_height = plugin_height - top_margin - btm_margin

    row_height = 30
    colors_y = 0


    def __init__(self):

        #Â init window
        self.main_window = Window((self.plugin_width, self.plugin_height), "Color Manager")

        # colors group
        self.main_window.colorsGroup = Group((self.lft_margin, self.top_margin, self.net_width, self.net_height))
        # iterating over colors dictionary
        for each_item in self.colors_order:
            # rgba
            red = self.colors_dict[each_item][0]
            green = self.colors_dict[each_item][1]
            blue = self.colors_dict[each_item][2]
            alpha = self.colors_dict[each_item][3]

            # declaring a row group
            rowGroup = Group((0, self.colors_y, self.net_width, self.row_height))

                # color caption
            colorName = TextBox((0,  5, self.net_width * .1, self.row_height), each_item)
            setattr(rowGroup, 'colorName%s' % (each_item), colorName)

                # color wheel
            colorWheel = ColorWell((self.net_width * .1 + self.col_margin, 0, self.net_width * .35, self.row_height),
                                   callback = self.colorWellEdit,
                                   color = NSColor.colorWithCalibratedRed_green_blue_alpha_(red, green, blue, alpha))
            setattr(rowGroup, 'colorWheel%s' % (each_item), colorWheel)

                # colors values
            colorValues = TextBox((self.net_width * .5 + self.col_margin, 5, self.net_width * .5, self.row_height),
                                  "R:%.0f, G:%.0f, B:%.0f, A:%.0f" % (red*255, green*255, blue*255, alpha*255))
            setattr(rowGroup, 'colorValues%s' % (each_item), colorValues)

                # append the group attribute
            setattr(self.main_window.colorsGroup, 'color%s' % (each_item), rowGroup)

            # next row!
            self.colors_y += self.row_height

        # closing window
        self.main_window.open()


    def colorWellEdit(self, sender):
        print sender.get()


if __name__ == '__main__':
    ColorManager()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment