Skip to content

Commit

Permalink
Allow tuple colors to be put directly into GanjaScene
Browse files Browse the repository at this point in the history
This means that the caller can now use any of the following in the `color` argument, all with the same meaning:

* `0x00FF0000`
* `(255, 0, 0)`
* `(0, 255, 0, 0)`
  • Loading branch information
eric-wieser committed Dec 9, 2019
1 parent 31a6551 commit 7b66c07
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
25 changes: 8 additions & 17 deletions pyganja/GanjaScene.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import json
import enum
from . import color as _color


def _as_list(mv_array):
Expand Down Expand Up @@ -41,12 +41,9 @@ def __add__(self, other):
else:
raise ValueError('The objects being added are not both GanjaScenes...')

def add_object(self, mv_array, color=int('AA000000', 16), label=None, static=False):
def add_object(self, mv_array, color=0xAA000000, label=None, static=False):
self.mv_length = len(mv_array)
if isinstance(color, enum.Enum):
self.internal_list.append(color.value)
else:
self.internal_list.append(color)
self.internal_list.append(_color.as_hex(color))
if static:
self.internal_list.append({'data': [_as_list(mv_array)]})
else:
Expand All @@ -58,11 +55,8 @@ def add_object(self, mv_array, color=int('AA000000', 16), label=None, static=Fal
except:
raise ValueError('Labels must be strings')

def add_facet(self, mv_list, color=int('AA000000', 16), label=None, static=False):
if isinstance(color, enum.Enum):
self.internal_list.append(color.value)
else:
self.internal_list.append(color)
def add_facet(self, mv_list, color=0xAA000000, label=None, static=False):
self.internal_list.append(_color.as_hex(color))
self.mv_length = len(mv_list[0])
facet_list = []
for mv_array in mv_list:
Expand All @@ -78,7 +72,7 @@ def add_facet(self, mv_list, color=int('AA000000', 16), label=None, static=False
except:
raise ValueError('Labels must be strings')

def add_facets(self, mv_list, color=int('AA000000', 16), label=None, static=False):
def add_facets(self, mv_list, color=0xAA000000, label=None, static=False):
for mv_array in mv_list:
self.add_facet(mv_array,color=color, label=label, static=static)
if label is not None:
Expand All @@ -88,11 +82,8 @@ def add_facets(self, mv_list, color=int('AA000000', 16), label=None, static=Fals
except:
raise ValueError('Labels must be strings')

def add_objects(self, mv_list, color=int('AA000000', 16), label=None, static=False):
if isinstance(color, enum.Enum):
self.internal_list.append(color.value)
else:
self.internal_list.append(color)
def add_objects(self, mv_list, color=0xAA000000, label=None, static=False):
self.internal_list.append(_color.as_hex(color))
static_list = []
self.mv_length = len(mv_list[0])
for mv_array in mv_list:
Expand Down
2 changes: 1 addition & 1 deletion pyganja/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

from .script_api import *
from .color import *
from .color import Color, rgb2hex
from ._version import __version__
51 changes: 37 additions & 14 deletions pyganja/color.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@

from enum import Enum
import numbers
from enum import IntEnum
import operator

def rgb2hex(x):
if len(x) == 3:
return int('%02x%02x%02x' %(int(x[0]),int(x[1]),int(x[2])),16)
elif len(x) == 4:
return int('%02x%02x%02x%02x' %(int(x[0]),int(x[1]),int(x[2]),int(x[3])),16)
if len(x) in (3, 4):
val = 0
for xi in x:
val = (val << 8) | _entry_as_uint8(xi)
return val
else:
raise ValueError('X must be of length 3 or 4, ie. an rgb or argb array')

class Color(Enum):
BLUE = int('000000FF', 16)
RED = int('00FF0000', 16)
GREEN = int('0000FF00', 16)
YELLOW = int('00FFFF00', 16)
MAGENTA = int('00FF00FF', 16)
CYAN = int('0000FFFF', 16)
BLACK = int('00000000', 16)

def _entry_as_uint8(v):
if isinstance(v, numbers.Integral):
if v > 255:
return 255
elif v < 0:
return 0
else:
return operator.index(v)
else:
raise TypeError("Can't convert {!r} to a color component".format(v))


def as_hex(x):
""" Convert any vague color description to a hex color """
try:
return operator.index(x)
except TypeError:
return rgb2hex(x)


class Color(IntEnum):
BLUE = 0x000000FF
RED = 0x00FF0000
GREEN = 0x0000FF00
YELLOW = 0x00FFFF00
MAGENTA = 0x00FF00FF
CYAN = 0x0000FFFF
BLACK = 0x00000000

0 comments on commit 7b66c07

Please sign in to comment.