From ef1d80f44e4b7014b64e1ed787b287de9efd230e Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Tue, 27 Apr 2010 20:46:08 +0000 Subject: adding gray level block --- diff --git a/NEWS b/NEWS index 79847a9..d05a37b 100644 --- a/NEWS +++ b/NEWS @@ -1,9 +1,12 @@ 87 +* added fill block +* added gray block * fixed typo in sample code * added mouse support to sample code (See http://tonyforster.blogspot.com/2010/03/mouse-support-in-turtleart.html) + 86 * More .es updates diff --git a/tacanvas.py b/tacanvas.py index dc325d3..28656aa 100644 --- a/tacanvas.py +++ b/tacanvas.py @@ -41,6 +41,11 @@ def calc_shade(c, s): return int(c*(1+s*.8)) return int(c+(65536-c)*s*.9) +def calc_gray(c, g): + if g == 100: + return c + return int(((c*g)+(32768*(100-g)))/100) + colors = {} DEGTOR = 2*pi/360 @@ -96,6 +101,7 @@ class TurtleGraphics: self.pensize = 5 self.tcolor = 0 self.color = 0 + self.gray = 100 self.fill = False self.poly_points = [] self.svg = SVG() @@ -139,6 +145,7 @@ class TurtleGraphics: self.canvas.images[0].draw_rectangle(self.gc, True, *rect) self.invalt(0, 0, self.width, self.height) self.setpensize(5, share) + self.setgray(100, share) self.setcolor(0, share) self.settextcolor(70) self.settextsize(48) @@ -322,6 +329,22 @@ class TurtleGraphics: self.tw.activity.send_event("c|%s" % \ (data_to_string([self.tw.nick, round_int(c)]))) + def setgray(self, g, share=True): + try: + self.gray = g + except: + pass + if self.gray < 0: + self.gray = 0 + if self.gray > 100: + self.gray = 100 + self.set_fgcolor() + self.set_textcolor() + self.tw.active_turtle.set_gray(self.gray) + if self.tw.sharing() and share: + self.tw.activity.send_event("g|%s" % \ + (data_to_string([self.tw.nick, round_int(self.gray)]))) + def settextcolor(self, c): try: self.tcolor = c @@ -367,6 +390,8 @@ class TurtleGraphics: sh = (wrap100(self.shade) - 50)/50.0 rgb = color_table[wrap100(self.color)] r, g, b = (rgb>>8)&0xff00, rgb&0xff00, (rgb<<8)&0xff00 + r, g, b = calc_gray(r, self.gray), calc_gray(g, self.gray),\ + calc_gray(b, self.gray) r, g, b = calc_shade(r, sh), calc_shade(g, sh), calc_shade(b, sh) self.fgrgb = [r>>8, g>>8, b>>8] self.fgcolor = self.cm.alloc_color(r, g, b) @@ -472,6 +497,7 @@ class TurtleGraphics: self.ycor = self.height/2 - ty - 30 self.heading = self.tw.active_turtle.get_heading() self.setcolor(self.tw.active_turtle.get_color(), False) + self.setgray(self.tw.active_turtle.get_gray(), False) self.setshade(self.tw.active_turtle.get_shade(), False) self.setpensize(self.tw.active_turtle.get_pen_size(), False) self.setpen(self.tw.active_turtle.get_pen_state(), False) diff --git a/taconstants.py b/taconstants.py index cc520f4..7bd52ec 100644 --- a/taconstants.py +++ b/taconstants.py @@ -120,7 +120,8 @@ PALETTES = [['clean', 'forward', 'back', 'show', 'left', 'right', 'arc', 'scale', 'leftpos', 'toppos', 'rightpos', 'bottompos'], ['penup','pendown', 'setpensize', 'fillscreen', 'pensize', - 'setcolor', 'setshade', 'color', 'shade'], + 'setcolor', 'setshade', 'setgray', 'color', 'shade', + 'gray', 'startfill', 'stopfill' ], [ 'red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple'], ['plus2', 'minus2', 'product2', 'division2', 'identity2', 'remainder2', 'sqrt', 'random', @@ -185,7 +186,7 @@ BASIC_STYLE_1ARG = ['forward', 'back', 'left', 'right', 'seth', 'show', 'image', 'setscale', 'setpensize', 'setcolor', 'setshade', 'print', 'showaligned', 'settextsize', 'settextcolor', 'print', 'wait', 'storeinbox1', 'savepix', 'storeinbox2', 'wait', 'stack', 'push', 'nop', 'addturtle', 'comment', - 'savesvg'] + 'savesvg', 'setgray'] BASIC_STYLE_VAR_ARG = ['userdefined', 'userdefined2args', 'userdefined3args'] BULLET_STYLE = ['templatelist', 'list'] BASIC_STYLE_2ARG = ['arc', 'setxy', 'fillscreen', 'storein', 'write'] @@ -194,7 +195,7 @@ BOX_STYLE = ['number', 'xcor', 'ycor', 'heading', 'pensize', 'color', 'shade', 'toppos', 'rightpos', 'bottompos', 'width', 'height', 'pop', 'keyboard', 'red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple', 'titlex', 'titley', 'leftx', 'topy', 'rightx', 'bottomy', - 'volume', 'pitch', 'voltage', 'resistance'] + 'volume', 'pitch', 'voltage', 'resistance', 'gray'] BOX_STYLE_MEDIA = ['description', 'audio', 'journal'] NUMBER_STYLE = ['plus2', 'product2', 'myfunc'] NUMBER_STYLE_VAR_ARG = ['myfunc1arg', 'myfunc2arg', 'myfunc3arg'] @@ -280,6 +281,7 @@ BLOCK_NAMES = { 'forever':[_('forever')], 'forward':[_('forward')], 'fullscreen':[_('full screen')], + 'gray':[_('gray')], 'greater2':[">"], 'green':[_('green')+' = 30'], 'hat':[_('action')], @@ -347,6 +349,7 @@ BLOCK_NAMES = { 'sandwichtop2':[_('top of stack')], 'scale':[_('scale')], 'setcolor':[_('set color')], + 'setgray':[_('set gray')], 'seth':[_('set heading')], 'setpensize':[_('set pen size')], 'setscale':[_('set scale')], @@ -425,6 +428,7 @@ PRIMITIVES = { 'forever':'forever', 'forward':'forward', 'fullscreen':'fullscreen', + 'gray':'gray', 'greater2':'greater?', 'green':'green', 'hat':'nop3', @@ -482,6 +486,7 @@ PRIMITIVES = { 'savesvg':'savesvg', 'scale':'scale', 'setcolor':'setcolor', + 'setgray':'setgray', 'seth':'seth', 'setpensize':'setpensize', 'setscale':'setscale', @@ -565,6 +570,7 @@ DEFAULTS = { 'savepix':[_('picture name')], 'savesvg':[_('picture name')], 'setcolor':[0], + 'setgray':[100], 'seth':[0], 'setpensize':[5], 'setscale':[33], @@ -718,6 +724,7 @@ HELP_STRINGS = { 'forever':_("loops forever"), 'forward':_("moves turtle forward"), 'fullscreen':_("hides the Sugar toolbars"), + 'gray':_("holds current gray level (can be used in place of a number block)"), 'greater2':_("logical greater-than operator"), 'hat1':_("top of Action 1 stack"), 'hat2':_("top of Action 2 stack"), @@ -785,6 +792,7 @@ HELP_STRINGS = { 'savesvg':_("saves turtle graphics as an SVG file in the Sugar Journal"), 'scale':_("holds current scale value"), '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.)"), 'setpensize':_("sets size of the line drawn by the turtle"), 'setscale':_("sets the scale of media"), diff --git a/talogo.py b/talogo.py index a060b5a..2a24ec3 100644 --- a/talogo.py +++ b/talogo.py @@ -263,6 +263,7 @@ class LogoCode: 'clean':[0, lambda self: self.prim_clear()], 'clearheap':[0, lambda self: self.empty_heap()], 'color':[0, lambda self: self.tw.canvas.color], + 'gray':[0, lambda self: self.tw.canvas.gray], 'comment':[1, lambda self, x: self.prim_print(x, True)], 'container':[1, lambda self,x: x], 'cyan':[0, lambda self: 50], @@ -322,6 +323,7 @@ class LogoCode: 'savesvg':[1, lambda self, x: self.save_svg(x)], 'scale':[0, lambda self: self.scale], '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)], 'setpensize':[1, lambda self, x: self.tw.canvas.setpensize(x)], 'setscale':[1, lambda self, x: self.set_scale(x)], diff --git a/taturtle.py b/taturtle.py index b6fc6a9..4eaa72f 100644 --- a/taturtle.py +++ b/taturtle.py @@ -112,6 +112,7 @@ class Turtle: self.heading = 0 self.pen_shade = 50 self.pen_color = 0 + self.pen_gray = 100 self.pen_size = 5 self.pen_state = True @@ -139,6 +140,10 @@ class Turtle: """ Set the pen color for this turtle. """ self.pen_color = color + def set_gray(self, gray): + """ Set the pen gray level for this turtle. """ + self.pen_gray = gray + def set_shade(self, shade): """ Set the pen shade for this turtle. """ self.pen_shade = shade @@ -182,6 +187,10 @@ class Turtle: """ Return the turtle's color. """ return(self.pen_color) + def get_gray(self): + """ Return the turtle's gray level. """ + return(self.pen_gray) + def get_shade(self): """ Return the turtle's shade. """ return(self.pen_shade) -- cgit v0.9.1