Skip to content

Commit

Permalink
Split one big file into several small
Browse files Browse the repository at this point in the history
  • Loading branch information
a-sk committed Jan 26, 2012
1 parent d2f1e07 commit 00b4dff
Show file tree
Hide file tree
Showing 9 changed files with 2,165 additions and 206 deletions.
68 changes: 68 additions & 0 deletions color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from named_colors import named_colors


class Color(object):
"""Convenience to work with colors"""

def __init__(self, color):
self.color = color

@property
def hex(self):
color = self.color
if color in named_colors:
hex_color = named_colors[color]
elif not color.startswith('#'):
# if rgb
color = color.split(',')
hex_color = self._rgb_to_hex(tuple(color))
else:
if len(color) == 4:
# 3 sign hex
color = "#{0[1]}{0[1]}{0[2]}{0[2]}{0[3]}{0[3]}".format(color)
hex_color = color

return hex_color

@property
def undash(self):
return self.hex.lstrip('#')

@property
def opposite(self):
r, g, b = self._hex_to_rgb(self.undash)
brightness = (r + r + b + b + g + g) / 6
if brightness > 130:
return '#000000'
else:
return '#ffffff'

def __repr__(self):
return self.hex

def __str__(self):
return self.hex

def __eq__(self, other):
return self.hex == other

def __hash__(self):
return hash(self.hex)

def _rgb_to_hex(self, rgb):
if str(rgb[0])[-1] == '%':
# percentage notation
r = int(rgb[0].rstrip('%')) * 255 / 100
g = int(rgb[1].rstrip('%')) * 255 / 100
b = int(rgb[2].rstrip('%')) * 255 / 100
return self._rgb_to_hex((r, g, b))

if len(rgb) == 4:
#rgba
rgb = rgb[0:3]

return '#%02x%02x%02x' % tuple(int(x) for x in rgb)

def _hex_to_rgb(self, hex):
hex_len = len(hex)
return tuple(int(hex[i:i + hex_len / 3], 16) for i in range(0, hex_len, hex_len / 3))
222 changes: 16 additions & 206 deletions css-colors.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
# stdlib
from glob import glob
from os import remove as rm
from os.path import join, basename, dirname, normpath, relpath, exists
from os.path import basename, exists
from plistlib import readPlist as read_plist
from plistlib import writePlist as write_plist
from random import randint
import re

# sublime
import sublime
import sublime_plugin

# local imports
from colors import named_colors
from theme import *
from state import State
from settings import Settings
from color import Color

# Constants
PACKAGES_PATH = sublime.packages_path()
SUBLIME_PATH = dirname(PACKAGES_PATH)

user_settings = Settings('CSS-colors.sublime-settings')


def clear_css_regions(view):
Expand All @@ -30,197 +29,6 @@ def clear_css_regions(view):
count = -1


class user_settings(object):
"""Global object represents user settings
"""

_settings_file = 'CSS-colors.sublime-settings'
_settings = sublime.load_settings(_settings_file)

class __metaclass__(type):

@property
def dynamic_highlight(cls):
# default to True
s = cls._settings.get('dynamic_highlight', True)
return s

@dynamic_highlight.setter
def dynamic_highlight(cls, value):
cls._settings.set('dynamic_highlight', value)
cls.save()

def save(cls):
sublime.save_settings(cls._settings_file)


class Color(object):
"""Convenience to work with colors"""

def __init__(self, color):
self.color = color

@property
def hex(self):
color = self.color
if color in named_colors:
hex_color = named_colors[color]
elif not color.startswith('#'):
# if rgb
color = color.split(',')
hex_color = self._rgb_to_hex(tuple(color))
else:
if len(color) == 4:
# 3 sign hex
color = "#{0[1]}{0[1]}{0[2]}{0[2]}{0[3]}{0[3]}".format(color)
hex_color = color

return hex_color

@property
def undash(self):
return self.hex.lstrip('#')

@property
def opposite(self):
r, g, b = self._hex_to_rgb(self.undash)
brightness = (r + r + b + b + g + g) / 6
if brightness > 130:
return '#000000'
else:
return '#ffffff'

def __repr__(self):
return self.hex

def __str__(self):
return self.hex

def __eq__(self, other):
return self.hex == other

def __hash__(self):
return hash(self.hex)

def _rgb_to_hex(self, rgb):
if str(rgb[0])[-1] == '%':
# percentage notation
r = int(rgb[0].rstrip('%')) * 255 / 100
g = int(rgb[1].rstrip('%')) * 255 / 100
b = int(rgb[2].rstrip('%')) * 255 / 100
return self._rgb_to_hex((r, g, b))

if len(rgb) == 4:
#rgba
rgb = rgb[0:3]

return '#%02x%02x%02x' % tuple(int(x) for x in rgb)

def _hex_to_rgb(self, hex):
hex_len = len(hex)
return tuple(int(hex[i:i + hex_len / 3], 16) for i in range(0, hex_len, hex_len / 3))


class State(object):
"""File state based on colors.
Uses hash of all colors in file to save state
"""

def __init__(self, colors, file_id):
self._settings = sublime.load_settings('Colorized.sublime-settings')
self.colors = colors
self.file_id = file_id

def save(self):
state = {self.file_id: {'colors': [str(x) for x in self.colors]}}
self._settings.set('state', state)

@property
def need_generate_new_color_file(self):
saved_colors = self.saved_state['colors']
if set(self.colors) - set(saved_colors):
return True

@property
def saved_state(self):
"""Returns saved colors if any
or empty state
"""

s = self._settings.get('state')
if not s or not s.get(self.file_id):
return {'colors': []}
return s[self.file_id]

def erase(self):
s = self._settings.get('state')
if s:
self._settings.erase('state')


#TODO: add fallbacks on errors
class theme(object):
"""Global object represents ST color scheme """

_settings = sublime.load_settings('Base File.sublime-settings')
_prefix = 'Colorized-'

class __metaclass__(type):
@property
def abspash(cls):
theme_path = cls._settings.get('color_scheme') or ""

if theme_path.startswith('Packages'):
theme_path = join(SUBLIME_PATH, theme_path)

return normpath(theme_path)

@property
def relpath(cls):
return relpath(cls.abspash, SUBLIME_PATH)

@property
def dirname(cls):
return dirname(cls.abspash)

@property
def name(cls):
return basename(cls.abspash)

@property
def is_colorized(cls):
if cls.name.startswith(cls._prefix):
return True

def set(cls, theme):
"""theme: abs or relpath to PACKAGES_PATH"""
cls._settings.set('color_scheme', theme)

@property
def colorized_path(cls):
return join(cls.dirname, cls.colorized_name)

@property
def uncolorized_path(cls):
return join(cls.dirname, cls.uncolorized_name)

@property
def uncolorized_name(cls):
if cls.is_colorized:
s = re.search(cls._prefix + "(\d+-)?(?P<Name>.*)", cls.name)
theme_name = s.group('Name')
return theme_name
return cls.name

@property
def colorized_name(cls):
r = str(randint(1, 10 ** 15)) + '-'
return cls._prefix + r + cls.uncolorized_name

def on_select_new_theme(cls, callback):
cls._settings.add_on_change('color_scheme', callback)


def template(color):
"""Template dict to use in color theme plist generating"""

Expand All @@ -247,17 +55,18 @@ def generate_color_theme(colors):

theme_path = theme.abspash
theme_plist = read_plist(theme_path)
colorized_theme_path = theme.colorized_path
colorized_theme_path = colorized_path(theme)

new_colors = (template(color) for color in set(colors))
for el in new_colors:
theme_plist['settings'].append(el)
write_plist(theme_plist, colorized_theme_path)

theme.set(colorized_theme_path)

if basename(theme_path).startswith('Colorized-'):
if is_colorized(theme):
# check may be wrong
theme.set(colorized_theme_path)
rm(theme_path)
# check if exists
rm(theme_path + '.cache')


Expand Down Expand Up @@ -291,6 +100,7 @@ def colorize_css(view, erase_state):
return
colorize_regions(view, color_regions, colors)
if state.need_generate_new_color_file:
print "Generating theme"
generate_color_theme(colors)
state.save()

Expand All @@ -313,7 +123,7 @@ def get_color_regions(view):


def colorize_if_not(view):
if not theme.is_colorized:
if not is_colorized(theme):
colorize_css(view, True)


Expand All @@ -336,8 +146,8 @@ def run(self, edit, erase_state=False):
class CssUncolorizeCommand(sublime_plugin.TextCommand):
def run(self, edit):
clear_css_regions(self.view)
if theme.is_colorized:
theme.set(theme.uncolorized_path)
if is_colorized(theme):
theme.set(uncolorized_path(uncolorized_path))
clean_themes_folder()


Expand Down
Loading

0 comments on commit 00b4dff

Please sign in to comment.