Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/TurtleArt
diff options
context:
space:
mode:
authorWalter Bender <walter@sugarlabs.org>2010-08-12 22:41:29 (GMT)
committer Walter Bender <walter@sugarlabs.org>2010-08-12 22:41:29 (GMT)
commitbc6ec37e87d81367f97157dfd3366f4938c8af00 (patch)
tree1a67a74c70977a1e8c5f45632fac32ddd93d1866 /TurtleArt
parentb3d151b60e285339a4f39708f5dce818151a8419 (diff)
Turtle 'sees'
Diffstat (limited to 'TurtleArt')
-rw-r--r--TurtleArt/tacanvas.py18
-rw-r--r--TurtleArt/taconstants.py7
-rw-r--r--TurtleArt/talogo.py7
3 files changed, 30 insertions, 2 deletions
diff --git a/TurtleArt/tacanvas.py b/TurtleArt/tacanvas.py
index 667cc0f..72c1c7d 100644
--- a/TurtleArt/tacanvas.py
+++ b/TurtleArt/tacanvas.py
@@ -566,6 +566,24 @@ class TurtleGraphics:
int(w), int(h)),
False)
+ def get_color_index(self, r, g, b, a=0):
+ """ Find the closest palette entry to the rgb triplet """
+ # TODO: Take into account gray and shade levels
+ min_distance = 1000000
+ closest_color = -1
+ for i, c in enumerate(color_table):
+ cr = int((c & 0xff0000) >> 16)
+ cg = int((c & 0x00ff00) >> 8)
+ cb = int((c & 0x0000ff))
+ distance_squared = ((cr - r) ** 2) + ((cg - g) ** 2) + \
+ ((cb - b) ** 2)
+ if distance_squared == 0:
+ return i
+ if distance_squared < min_distance:
+ min_distance = distance_squared
+ closest_color = i
+ return closest_color
+
def get_pixel(self):
""" Read the pixel at x, y """
if self.tw.interactive_mode:
diff --git a/TurtleArt/taconstants.py b/TurtleArt/taconstants.py
index 5088b8f..b20948d 100644
--- a/TurtleArt/taconstants.py
+++ b/TurtleArt/taconstants.py
@@ -135,7 +135,7 @@ PALETTES = [['clean', 'forward', 'back', 'show', 'left', 'right',
['kbinput', 'push', 'printheap', 'keyboard', 'pop', 'clearheap',
'myfunc1arg', 'userdefined', 'addturtle', 'comment', 'print',
'cartesian', 'width', 'height', 'polar', 'sandwichtop',
- 'sandwichbottom', 'readpixel'],
+ 'sandwichbottom', 'readpixel', 'see'],
['journal', 'audio', 'description', 'hideblocks', 'showblocks',
'fullscreen', 'savepix', 'savesvg', 'picturelist',
'picture1x1a', 'picture1x1', 'picture2x2', 'picture2x1',
@@ -199,7 +199,7 @@ BOX_STYLE = ['number', 'xcor', 'ycor', 'heading', 'pensize', 'color', 'shade',
'toppos', 'rightpos', 'bottompos', 'width', 'height', 'pop', 'keyboard',
'red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple', 'white',
'black', 'titlex', 'titley', 'leftx', 'topy', 'rightx', 'bottomy',
- 'volume', 'pitch', 'voltage', 'resistance', 'gray']
+ 'volume', 'pitch', 'voltage', 'resistance', 'gray', 'see']
BOX_STYLE_MEDIA = ['description', 'audio', 'journal']
NUMBER_STYLE = ['plus2', 'product2', 'myfunc']
NUMBER_STYLE_VAR_ARG = ['myfunc1arg', 'myfunc2arg', 'myfunc3arg']
@@ -354,6 +354,7 @@ BLOCK_NAMES = {
'sandwichtop':[_('top of stack')],
'sandwichtop2':[_('top of stack')],
'scale':[_('scale')],
+ 'see':[_('turtle sees')],
'setcolor':[_('set color')],
'setgray':[_('set gray')],
'seth':[_('set heading')],
@@ -493,6 +494,7 @@ PRIMITIVES = {
'sandwichcollapsed':'nop',
'savepix':'savepix',
'savesvg':'savesvg',
+ 'see':'see',
'scale':'scale',
'setcolor':'setcolor',
'setgray':'setgray',
@@ -802,6 +804,7 @@ HELP_STRINGS = {
'savepix':_("saves a picture to the Sugar Journal"),
'savesvg':_("saves turtle graphics as an SVG file in the Sugar Journal"),
'scale':_("holds current scale value"),
+ 'see':_('returns the color that the turtle "sees"'),
'setcolor':_("sets color of the line drawn by the turtle"),
'setgray':_("sets gray level of the line drawn by the turtle"),
'seth':_("sets the heading of the turtle (0 is towards the top of the screen.)"),
diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py
index 84da6d3..5f2a6d5 100644
--- a/TurtleArt/talogo.py
+++ b/TurtleArt/talogo.py
@@ -324,6 +324,7 @@ class LogoCode:
'savepix':[1, lambda self, x: self.save_picture(x)],
'savesvg':[1, lambda self, x: self.save_svg(x)],
'scale':[0, lambda self: self.scale],
+ 'see':[0, lambda self: self.see()],
'setcolor':[1, lambda self, x: self.tw.canvas.setcolor(x)],
'setgray':[1, lambda self, x: self.tw.canvas.setgray(x)],
'seth':[1, lambda self, x: self.tw.canvas.seth(x)],
@@ -1171,6 +1172,12 @@ class LogoCode:
self.tw.canvas.draw_text(text, int(x), int(y),
self.body_height, int(w))
+ def see(self):
+ """ Read r, g, b from the canvas and return a corresponding palette
+ color """
+ r, g, b, a = self.tw.canvas.get_pixel()
+ return self.tw.canvas.get_color_index(r, g, b)
+
def read_pixel(self):
""" Read r, g, b, a from the canvas and push b, g, r to the stack """
r, g, b, a = self.tw.canvas.get_pixel()