Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarion <marion.zepf@gmail.com>2013-08-10 11:29:17 (GMT)
committer Marion <marion.zepf@gmail.com>2013-08-10 11:29:17 (GMT)
commit95c0a8dd49712242efd7127e4c2c66b3ca88dd8d (patch)
tree102074657de6219d25210b410fe2396f20672244
parent8aa739c8a5bd801ec03896efc2b4384827fed1df (diff)
parent9c931881289528c4fd6ed4406d3a8ae59efebfb9 (diff)
Merge branch 'color-class' into primitive-class
Conflicts: TurtleArt/tabasics.py -- merge import statements TurtleArt/tablock.py -- ditto
-rw-r--r--TurtleArt/tabasics.py29
-rw-r--r--TurtleArt/tablock.py8
-rw-r--r--TurtleArt/tacanvas.py18
-rw-r--r--TurtleArt/taconstants.py58
-rw-r--r--TurtleArt/taexportlogo.py14
-rw-r--r--TurtleArt/taturtle.py54
-rw-r--r--plugins/turtle_blocks_extras/turtle_blocks_extras.py30
7 files changed, 108 insertions, 103 deletions
diff --git a/TurtleArt/tabasics.py b/TurtleArt/tabasics.py
index 2d16c2d..379718d 100644
--- a/TurtleArt/tabasics.py
+++ b/TurtleArt/tabasics.py
@@ -71,18 +71,11 @@ from gettext import gettext as _
from tapalette import (make_palette, define_logo_function)
from talogo import (primitive_dictionary, logoerror)
from tautils import (convert, chr_to_ord, round_int, strtype)
-from taconstants import (COLORDICT, CONSTANTS)
+from taconstants import (Color, CONSTANTS)
from taprimitive import Primitive
from taturtle import Turtle
-def _color_to_num(c):
- if COLORDICT[c][0] is None:
- return(COLORDICT[c][1])
- else:
- return(COLORDICT[c][0])
-
-
def _num_type(x):
''' Is x a number type? '''
if isinstance(x, (int, float)):
@@ -1351,10 +1344,10 @@ variable'))
def _prim_plus(self, x, y):
''' Add numbers, concat strings '''
- if x in COLORDICT:
- x = _color_to_num(x)
- if y in COLORDICT:
- y = _color_to_num(y)
+ if isinstance(x, Color):
+ x = int(x)
+ if isinstance(y, Color):
+ y = int(y)
if _num_type(x) and _num_type(y):
return(x + y)
elif isinstance(x, list) and isinstance(y, list):
@@ -1463,8 +1456,8 @@ variable'))
pass
if isinstance(x, list):
raise logoerror("#syntaxerror")
- if x in COLORDICT:
- return _color_to_num(x)
+ if isinstance(x, Color):
+ return int(x)
xx = convert(x.replace(self.tw.decimal_point, '.'), float)
if isinstance(xx, float):
return xx
@@ -1477,12 +1470,12 @@ variable'))
def _make_constant(self, palette, block_name, label, constant):
''' Factory for constant blocks '''
- if constant in COLORDICT:
- if COLORDICT[constant][0] is not None:
- value = str(COLORDICT[constant][0])
+ if isinstance(constant, Color):
+ if constant.color is not None:
+ value = str(constant.color)
else:
# Black or White
- value = '0 tasetshade %d' % (COLORDICT[constant][1])
+ value = '0 tasetshade %d' % (constant.shade)
else:
value = constant
palette.add_block(block_name,
diff --git a/TurtleArt/tablock.py b/TurtleArt/tablock.py
index e878a46..13f50ae 100644
--- a/TurtleArt/tablock.py
+++ b/TurtleArt/tablock.py
@@ -24,7 +24,7 @@ import cairo
from taconstants import (EXPANDABLE, EXPANDABLE_ARGS, OLD_NAMES, CONSTANTS,
STANDARD_STROKE_WIDTH, BLOCK_SCALE, BOX_COLORS,
- GRADIENT_COLOR, EXPANDABLE_FLOW, COLORDICT,
+ GRADIENT_COLOR, EXPANDABLE_FLOW, Color,
PREFIX_DICTIONARY)
from tapalette import (palette_blocks, block_colors, expandable_blocks,
content_blocks, block_names, block_primitives,
@@ -564,10 +564,8 @@ class Block:
else:
self._set_labels(i, str(v))
elif self.type == 'block' and self.name in CONSTANTS:
- if CONSTANTS[self.name] in COLORDICT:
- v = COLORDICT[CONSTANTS[self.name]][0]
- if v is None:
- v = COLORDICT[CONSTANTS[self.name]][1]
+ if isinstance(CONSTANTS[self.name], Color):
+ v = int(CONSTANTS[self.name])
else:
v = CONSTANTS[self.name]
self._set_labels(0, block_names[self.name][0] + ' = ' + str(v))
diff --git a/TurtleArt/tacanvas.py b/TurtleArt/tacanvas.py
index 984e1cf..4bac442 100644
--- a/TurtleArt/tacanvas.py
+++ b/TurtleArt/tacanvas.py
@@ -28,7 +28,7 @@ import cairo
import pangocairo
from tautils import get_path
-from taconstants import COLORDICT, TMP_SVG_PATH
+from taconstants import Color, TMP_SVG_PATH
def wrap100(n):
@@ -208,19 +208,19 @@ class TurtleGraphics:
save_rgb = self._fgrgb[:]
# Special case for color blocks
- if color in COLORDICT:
- if COLORDICT[color][0] is None:
- self._shade = COLORDICT[color][1]
+ if isinstance(color, Color):
+ if color.color is None:
+ self._shade = color.shade
else:
- self._color = COLORDICT[color][0]
+ self._color = color.color
else:
self._color = color
- if shade in COLORDICT:
- self._shade = COLORDICT[shade][1]
+ if isinstance(shade, Color):
+ self._shade = shade.shade
else:
self._shade = shade
- if gray in COLORDICT:
- self._gray = COLORDICT[gray][2]
+ if isinstance(gray, Color):
+ self._gray = gray.gray
else:
self._gray = gray
diff --git a/TurtleArt/taconstants.py b/TurtleArt/taconstants.py
index 835209e..b1dc43e 100644
--- a/TurtleArt/taconstants.py
+++ b/TurtleArt/taconstants.py
@@ -79,20 +79,60 @@ XO4 = 'xo4'
UNKNOWN = 'unknown'
TMP_SVG_PATH = '/tmp/turtle_output.svg'
+
+
+class Color(object):
+ """ A color used in block programs (e.g., as pen color). """
+
+ def __init__(self, name, color=0, shade=50, gray=100):
+ """ name -- a string with the name of the color, e.g., 'red'
+ color -- the hue (0-100, or None for white, gray, and black)
+ shade -- the lightness (0 is black, 100 is white)
+ gray -- the saturation (0 is gray, 100 is fully saturated) """
+ self.name = name
+ self.color = color
+ self.shade = shade
+ self.gray = gray
+
+ def __int__(self):
+ if self.color is None:
+ return int(self.shade)
+ else:
+ return int(self.color)
+
+ def __float__(self):
+ return float(int(self))
+
+ def __str__(self):
+ return str(self.name)
+
+ def __repr__(self):
+ return '%s (%s/%d/%d)' % (str(self.name), str(self.color),
+ self.shade, self.gray)
+
+ # TODO implement addition
+
+ def is_gray(self):
+ """ Return True iff this color is white, gray, or black, i.e. if its
+ hue is not set or its saturation is zero. """
+ return self.color is None or not self.gray
+
+
+
CONSTANTS = {'leftpos': None, 'toppos': None, 'rightpos': None,
'bottompos': None, 'width': None, 'height': None,
- 'black': '_black', 'white': '_white', 'red': '_red',
- 'orange': '_orange', 'yellow': '_yellow', 'green': '_green',
- 'cyan': '_cyan', 'blue': '_blue', 'purple': '_purple',
+ 'black': Color('black', None, 0, 0),
+ 'white': Color('white', None, 100, 0),
+ 'red': Color('red', 0, 50, 100),
+ 'orange': Color('orange', 10, 50, 100),
+ 'yellow': Color('yellow', 20, 50, 100),
+ 'green': Color('green', 40, 50, 100),
+ 'cyan': Color('cyan', 50, 50, 100),
+ 'blue': Color('blue', 70, 50, 100),
+ 'purple': Color('purple', 90, 50, 100),
'titlex': None, 'titley': None, 'leftx': None,
'topy': None, 'rightx': None, 'bottomy': None}
-COLORDICT = {'_black': [None, 0, 0], '_white': [None, 100, 0],
- '_red': [0, 50, 100], '_orange': [10, 50, 100],
- '_yellow': [20, 50, 100], '_green': [40, 50, 100],
- '_cyan': [50, 50, 100], '_blue': [70, 50, 100],
- '_purple': [90, 50, 100]}
-
# Blocks that are expandable
EXPANDABLE_STYLE = ['boolean-style', 'compare-porch-style', 'compare-style',
'number-style-porch', 'number-style', 'basic-style-2arg',
diff --git a/TurtleArt/taexportlogo.py b/TurtleArt/taexportlogo.py
index f021f94..4a1ed4f 100644
--- a/TurtleArt/taexportlogo.py
+++ b/TurtleArt/taexportlogo.py
@@ -223,31 +223,31 @@ def _bottomy(tw):
def _red(tw):
- return CONSTANTS['red']
+ return '_' + str(CONSTANTS['red'])
def _orange(tw):
- return CONSTANTS['orange']
+ return '_' + str(CONSTANTS['orange'])
def _yellow(tw):
- return CONSTANTS['yellow']
+ return '_' + str(CONSTANTS['yellow'])
def _green(tw):
- return CONSTANTS['green']
+ return '_' + str(CONSTANTS['green'])
def _cyan(tw):
- return CONSTANTS['cyan']
+ return '_' + str(CONSTANTS['cyan'])
def _blue(tw):
- return CONSTANTS['blue']
+ return '_' + str(CONSTANTS['blue'])
def _purple(tw):
- return CONSTANTS['purple']
+ return '_' + str(CONSTANTS['purple'])
def _white(tw):
diff --git a/TurtleArt/taturtle.py b/TurtleArt/taturtle.py
index 3d9d730..16820f8 100644
--- a/TurtleArt/taturtle.py
+++ b/TurtleArt/taturtle.py
@@ -28,7 +28,7 @@ import cairo
from random import uniform
from math import sin, cos, pi, sqrt
from taconstants import (TURTLE_LAYER, DEFAULT_TURTLE_COLORS, DEFAULT_TURTLE,
- COLORDICT)
+ Color)
from tasprite_factory import SVG, svg_str_to_pixbuf
from tacanvas import wrap100, COLOR_TABLE
from sprites import Sprite
@@ -319,13 +319,7 @@ class Turtle:
def set_heading(self, heading, share=True):
''' Set the turtle heading (one shape per 360/SHAPES degrees) '''
- try:
- self._heading = heading
- except (TypeError, ValueError):
- debug_output('bad value sent to %s' % (__name__),
- self._turtles.turtle_window.running_sugar)
- return
- self._heading %= 360
+ self._heading = heading % 360
i = (int(self._heading + 5) % 360) / (360 / SHAPES)
if not self._hidden and self.spr is not None:
@@ -341,25 +335,20 @@ class Turtle:
def set_color(self, color=None, share=True):
''' Set the pen color for this turtle. '''
+ if color is None:
+ color = self._pen_color
# Special case for color blocks
- if color is not None and color in COLORDICT:
- self.set_shade(COLORDICT[color][1], share)
- self.set_gray(COLORDICT[color][2], share)
- if COLORDICT[color][0] is not None:
- self.set_color(COLORDICT[color][0], share)
- color = COLORDICT[color][0]
+ elif isinstance(color, Color):
+ self.set_shade(color.shade, share)
+ self.set_gray(color.gray, share)
+ if color.color is not None:
+ color = color.color
else:
color = self._pen_color
- elif color is None:
- color = self._pen_color
- try:
- self._pen_color = color
- except (TypeError, ValueError):
- debug_output('bad value sent to %s' % (__name__),
- self._turtles.turtle_window.running_sugar)
- return
+ self._pen_color = color
+ # TODO replace these three attributes with one reference to a Color
self._turtles.turtle_window.canvas.set_fgcolor(shade=self._pen_shade,
gray=self._pen_gray,
color=self._pen_color)
@@ -372,12 +361,7 @@ class Turtle:
def set_gray(self, gray=None, share=True):
''' Set the pen gray level for this turtle. '''
if gray is not None:
- try:
- self._pen_gray = gray
- except (TypeError, ValueError):
- debug_output('bad value sent to %s' % (__name__),
- self._turtles.turtle_window.running_sugar)
- return
+ self._pen_gray = gray
if self._pen_gray < 0:
self._pen_gray = 0
@@ -396,12 +380,7 @@ class Turtle:
def set_shade(self, shade=None, share=True):
''' Set the pen shade for this turtle. '''
if shade is not None:
- try:
- self._pen_shade = shade
- except (TypeError, ValueError):
- debug_output('bad value sent to %s' % (__name__),
- self._turtles.turtle_window.running_sugar)
- return
+ self._pen_shade = shade
self._turtles.turtle_window.canvas.set_fgcolor(shade=self._pen_shade,
gray=self._pen_gray,
@@ -415,12 +394,7 @@ class Turtle:
def set_pen_size(self, pen_size=None, share=True):
''' Set the pen size for this turtle. '''
if pen_size is not None:
- try:
- self._pen_size = max(0, pen_size)
- except (TypeError, ValueError):
- debug_output('bad value sent to %s' % (__name__),
- self._turtles.turtle_window.running_sugar)
- return
+ self._pen_size = max(0, pen_size)
self._turtles.turtle_window.canvas.set_pen_size(self._pen_size)
diff --git a/plugins/turtle_blocks_extras/turtle_blocks_extras.py b/plugins/turtle_blocks_extras/turtle_blocks_extras.py
index b618d9e..d6d4b29 100644
--- a/plugins/turtle_blocks_extras/turtle_blocks_extras.py
+++ b/plugins/turtle_blocks_extras/turtle_blocks_extras.py
@@ -33,7 +33,7 @@ from TurtleArt.talogo import (primitive_dictionary, logoerror,
from TurtleArt.taconstants import (DEFAULT_SCALE, ICON_SIZE, CONSTANTS,
MEDIA_SHAPES, SKIN_PATHS, BLOCKS_WITH_SKIN,
PYTHON_SKIN, PREFIX_DICTIONARY, VOICES,
- MACROS, COLORDICT)
+ MACROS, Color)
from TurtleArt.tautils import (round_int, debug_output, get_path,
data_to_string, find_group, image_to_base64,
hat_on_top, listify, data_from_file)
@@ -1091,20 +1091,20 @@ Journal objects'))
""" Print object n """
if flag and (self.tw.hide or self.tw.step_time == 0):
return
- if type(n) == list:
+ if isinstance(n, list):
self.tw.showlabel('print', n)
- elif type(n) == str or type(n) == unicode:
- if n in COLORDICT:
- if COLORDICT[n][0] is None:
- self.tw.showlabel('print', '%s %d, %s %d' %
- (_('shade'), COLORDICT[n][1],
- _('gray'), COLORDICT[n][2]))
- else:
- self.tw.showlabel('print', '%s %d, %s %d, %s %d' %
- (_('color'), COLORDICT[n][0],
- _('shade'), COLORDICT[n][1],
- _('gray'), COLORDICT[n][2]))
- elif n[0:6] == 'media_' and \
+ elif isinstance(n, Color):
+ if n.color is None:
+ self.tw.showlabel('print', '%s %d, %s %d' %
+ (_('shade'), n.shade,
+ _('gray'), n.gray))
+ else:
+ self.tw.showlabel('print', '%s %d, %s %d, %s %d' %
+ (_('color'), n.color,
+ _('shade'), n.shade,
+ _('gray'), n.gray))
+ elif isinstance(n, basestring):
+ if n[0:6] == 'media_' and \
n[6:].lower not in media_blocks_dictionary:
try:
if self.tw.running_sugar:
@@ -1122,7 +1122,7 @@ Journal objects'))
self.tw.showlabel('print', n)
else:
self.tw.showlabel('print', n)
- elif type(n) == int:
+ elif isinstance(n, int):
self.tw.showlabel('print', n)
else:
self.tw.showlabel(