ColabTurtlePlus 2.1.1__tar.gz → 2.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -39,6 +39,14 @@ Added addcomponent() to create new polygonal turtles. Because turtles are define
39
39
  new functions, addellipsecomponent() and addpathcomponent, to create turtles bassed on ellipical shapes and svg
40
40
  paths. To be consistent with Python's turtle, these are first invoked using a call to Shape("compound") and then
41
41
  combined using register_shape().
42
+
43
+ v2.1.2 September 2026
44
+ Fixed an issue introduced with register_shape when two or more turtles had the same shape.
45
+
46
+ v2.2 October 2026
47
+ Fixed some bugs related to color processing and speed.
48
+ Added colormode to handle RGB either in [0,1] range or [1,255] range.
49
+ Modified jumpto to mimic the teleport function added to the turtle module in Python 3.12
42
50
  """
43
51
 
44
52
  DEFAULT_WINDOW_SIZE = (800, 600)
@@ -78,6 +86,7 @@ VALID_MODES = ('standard','logo','world','svg')
78
86
  DEFAULT_TURTLE_SHAPE = 'classic'
79
87
  VALID_TURTLE_SHAPES = {'turtle', 'ring', 'classic', 'arrow', 'square', 'triangle', 'circle', 'turtle2', 'blank', 'user'}
80
88
  DEFAULT_MODE = 'standard'
89
+ DEFAULT_COLORMODE = 255
81
90
  DEFAULT_ANGLE_MODE = 'degrees'
82
91
  DEFAULT_POINTS = '-5,-4.5 0,-2.5 5,-4.5 0,4.5'
83
92
  DEFAULT_NAME = 'classic'
@@ -120,7 +129,6 @@ TURTLE_TURTLE2_SVG_TEMPLATE = """<g id="turtle2" visibility="{visibility}" trans
120
129
  TURTLE_USER_SVG_TEMPLATE = """<g id="{id}" visibility="{visibility}" transform="rotate({degrees},{rotation_x},{rotation_y}) translate({turtle_x}, {turtle_y})">
121
130
  <polygon points="{points}" transform="skewX({sk}) scale({sx},{sy})" style="stroke:{pcolor};fill:{turtle_color};stroke-width:{pw}" />
122
131
  </g>"""
123
-
124
132
  TURTLE_COMPONENT_SVG_TEMPLATE = """<g id="user" visibility="{visibility}" transform="rotate({degrees},{rotation_x},{rotation_y}) translate({turtle_x}, {turtle_y})">
125
133
  "{component}"
126
134
  </g>"""
@@ -160,6 +168,7 @@ class _Screen:
160
168
  self._turtles = []
161
169
  self.window_size = DEFAULT_WINDOW_SIZE
162
170
  self._mode = DEFAULT_MODE
171
+ self._colormode = DEFAULT_COLORMODE
163
172
  if self._mode in ['standard','logo']:
164
173
  self.xmin,self.ymin,self.xmax,self.ymax = -self.window_size[0]/2,-self.window_size[1]/2,self.window_size[0]/2,self.window_size[1]/2
165
174
  self.xscale = self.yscale = 1
@@ -218,7 +227,7 @@ class _Screen:
218
227
  degrees -= 90
219
228
 
220
229
 
221
- svg = shapeDict[turtle.turtle_shape].format(
230
+ svg = turtle.shapeDict[turtle.turtle_shape].format(
222
231
  turtle_color=turtle.fill_color,
223
232
  pcolor=turtle.pen_color,
224
233
  turtle_x=turtle_x,
@@ -401,10 +410,11 @@ class _Screen:
401
410
  turtle_x="{turtle_x}",
402
411
  turtle_y="{turtle_y}",
403
412
  )
404
- # componentDict[shape] = tm
405
413
  name = name.lower()
406
414
  VALID_TURTLE_SHAPES.add(name)
407
415
  shapeDict[name] = tmp
416
+ for turtle in self._turtles:
417
+ turtle.shapeDict = shapeDict.copy()
408
418
  addshape=register_shape
409
419
 
410
420
  #=========================
@@ -496,7 +506,10 @@ class _Screen:
496
506
  pcolor=turtle.pen_color,
497
507
  pwidth=turtle.pen_width)
498
508
  if turtle.is_filling:
499
- turtle.svg_fill_string += """ L {x1} {y1} """.format(x1=new_pos[0],y1=new_pos[1])
509
+ if turtle.is_pen_down:
510
+ turtle.svg_fill_string += """ L {x1} {y1} """.format(x1=new_pos[0],y1=new_pos[1])
511
+ else:
512
+ turtle.svg_fill_string += """ M {x1} {y1} """.format(x1=new_pos[0],y1=new_pos[1])
500
513
  turtle.turtle_pos = new_pos
501
514
  turtle.timeout = timeout_orig
502
515
  if not turtle.animate: self._updateDrawing(turtle=turtle)
@@ -766,6 +779,26 @@ class _Screen:
766
779
  self.xscale = self.yscale = 1
767
780
  self.resetscreen()
768
781
 
782
+ # Set color mode (1 or 255). If mode is not given, current mode is returned.
783
+ def colormode(self, cmode=None):
784
+ """Return the colormode or set it to 1.0 or 255.
785
+
786
+ Optional argument:
787
+ cmode -- one of the values 1.0 or 255
788
+
789
+ r, g, b values of colortriples have to be in range 0..cmode.
790
+
791
+ """
792
+ if cmode is None:
793
+ return self._colormode
794
+ if cmode == 1.0:
795
+ self._colormode = float(cmode)
796
+ elif cmode == 255:
797
+ self._colormode = int(cmode)
798
+ else:
799
+ err = f'Colormode {cmode} is invalid. It must be 1.0 or 255.'
800
+ raise ValueError(err)
801
+
769
802
  # Set up user-defined coordinate system using lower left and upper right corners.
770
803
  # Screen is reset.
771
804
  # if the xscale and yscale are not equal, the aspect ratio of the axes and the
@@ -840,33 +873,54 @@ class _Screen:
840
873
  return True
841
874
  return False
842
875
 
843
- # Used to validate if a 3 tuple of integers is a valid RGB color
876
+ # Used to validate if a 3 tuple of integers or a 3 tuple of floats is a valid RGB color
844
877
  def _validateColorTuple(self, color):
845
878
  if len(color) != 3:
846
879
  return False
847
- if not isinstance(color[0], int) or not isinstance(color[1], int) or not isinstance(color[2], int):
848
- return False
849
- if not 0 <= color[0] <= 255 or not 0 <= color[1] <= 255 or not 0 <= color[2] <= 255:
850
- return False
851
- return True
880
+ if self._colormode == 255:
881
+ if not isinstance(color[0], int) or not isinstance(color[1], int) or not isinstance(color[2], int):
882
+ return False
883
+ if not 0 <= color[0] <= 255 or not 0 <= color[1] <= 255 or not 0 <= color[2] <= 255:
884
+ return False
885
+ return True
886
+ if self._colormode == 1.0:
887
+ if not isinstance(color[0], (float,int)) or not isinstance(color[1], (float,int)) or not isinstance(color[2], (float,int)):
888
+ return False
889
+ if not 0 <= color[0] <= 1 or not 0 <= color[1] <= 1 or not 0 <= color[2] <= 1:
890
+ return False
891
+ return True
852
892
 
853
- # Helps validate color input to functions
854
- def _processColor(self,color):
893
+ # Helps validate color input to functions
894
+ def _processColor(self, color):
855
895
  if isinstance(color, str):
856
- if color == "": color = "none"
896
+ if color == "":
897
+ color = "none"
857
898
  color = color.lower().strip()
858
- if 'rgb' not in color: color = color.replace(" ","")
899
+ if 'rgb' not in color:
900
+ color = color.replace(" ", "")
859
901
  if not self._validateColorString(color):
860
- err = 'Color ' + color + ' is invalid. It can be a known html color name, 3-6 digit hex string, or rgb string.'
861
- raise ValueError(err)
862
- return color
902
+ # Use an f-string
903
+ err = f'Color {color} is invalid. It can be a known html color name, 3 or 6 digit hex string, or rgb string.'
904
+ err2 = "See https://www.w3schools.com/colors/colors_names.asp for known html color names."
905
+ raise ValueError(err + "\n" + err2)
906
+ return color
863
907
  elif isinstance(color, tuple):
864
908
  if not self._validateColorTuple(color):
865
- err = 'Color tuple ' + color + ' is invalid. It must be a tuple of three integers, which are in the interval [0,255]'
909
+ # Use an f-string to safely convert the tuple to text
910
+ endpt = self._colormode
911
+ if self._colormode == 255:
912
+ numtype = "integers"
913
+ else:
914
+ numtype = "floats"
915
+ err = f'Color tuple {color} is invalid. It must be a tuple of three {numtype} in the interval [0,{endpt}]'
866
916
  raise ValueError(err)
867
- return 'rgb(' + str(color[0]) + ',' + str(color[1]) + ',' + str(color[2]) + ')'
917
+ if self._colormode == 255:
918
+ return 'rgb(' + str(color[0]) + ',' + str(color[1]) + ',' + str(color[2]) + ')'
919
+ else:
920
+ return 'rgb(' + str(int(255.0*color[0])) + ',' + str(int(255.0*color[1])) + ',' + str(int(255.0*color[2])) + ')'
868
921
  else:
869
- err = 'The color parameter ' + color + ' must be a color string or a tuple'
922
+ # Use an f-string to safely handle any unknown data type (e.g. None, int)
923
+ err = f'The color parameter {color} must be a color string or a tuple'
870
924
  raise ValueError(err)
871
925
 
872
926
  #----------------------------------------------------------------------------------------------
@@ -974,8 +1028,7 @@ class Shape(object):
974
1028
  #-------------------------------------------------------------
975
1029
 
976
1030
  #----------------------------------------------------------------------------------------------
977
-
978
-
1031
+
979
1032
  class RawTurtle:
980
1033
 
981
1034
  def __init__(self, window=None):
@@ -1019,8 +1072,8 @@ class RawTurtle:
1019
1072
  self.stampnum = 0
1020
1073
  self.stamplist=[]
1021
1074
  self.points = DEFAULT_POINTS
1022
-
1023
- if screen._mode == "svg": shapeDict.update({"circle":TURTLE_RING_SVG_TEMPLATE})
1075
+ self.shapeDict = shapeDict.copy()
1076
+ if screen._mode == "svg": self.shapeDict.update({"circle":TURTLE_RING_SVG_TEMPLATE})
1024
1077
  screen._add(self)
1025
1078
 
1026
1079
 
@@ -1096,7 +1149,7 @@ class RawTurtle:
1096
1149
  self.screen._updateDrawing(turtle=self)
1097
1150
  elif self.turtle_shape != 'ring' and self.stretchfactor[0]==self.stretchfactor[1]:
1098
1151
  stretchfactor_orig = self.stretchfactor
1099
- template = shapeDict[self.turtle_shape]
1152
+ template = self.shapeDict[self.turtle_shape]
1100
1153
  tmp = """<animateTransform id = "one" attributeName="transform"
1101
1154
  type="scale"
1102
1155
  from="{sx} {sy}" to="{sx} {sy}"
@@ -1113,13 +1166,13 @@ class RawTurtle:
1113
1166
  fill="freeze"
1114
1167
  /></g>""".format(extent=deg, t=self.timeout*abs(deg)/90, sx=self.stretchfactor[0], sy=self.stretchfactor[1])
1115
1168
  newtemplate = template.replace("</g>",tmp)
1116
- shapeDict.update({self.turtle_shape:newtemplate})
1169
+ self.shapeDict.update({self.turtle_shape:newtemplate})
1117
1170
  self.stretchfactor = 1,1
1118
1171
  self.timeout = self.timeout*abs(deg)/90+0.001
1119
1172
  self.screen._updateDrawing(self)
1120
1173
  self.turtle_degree = (self.turtle_degree + deg) % 360
1121
1174
  self.turtle_orient = self._turtleOrientation()
1122
- shapeDict.update({self.turtle_shape:template})
1175
+ self.shapeDict.update({self.turtle_shape:template})
1123
1176
  self.stretchfactor = stretchfactor_orig
1124
1177
  self.timeout = timeout_orig
1125
1178
  else: #_turtle_shape == 'ring' or _stretchfactor[0] != _stretchfactor[1]
@@ -1376,26 +1429,52 @@ class RawTurtle:
1376
1429
  setposition = goto # alias
1377
1430
 
1378
1431
  # jump to a point without drawing or animation
1379
- def jumpto(self,x,y=None):
1380
- """Jumps to a specified point without drawing/animation
1381
-
1382
- Args:
1383
- x: a number or a pair of numbers
1384
- y: a number or None
1385
-
1386
- jumpto(x, y) or jumpto((x,y))
1432
+ def jumpto(self, x=None, y=None, fill_gap: bool = False) -> None:
1433
+ """Instantly move turtle to an absolute position.
1434
+
1435
+ Arguments:
1436
+ x -- a number or None
1437
+ y -- a number None
1438
+ fill_gap -- a boolean This argument must be specified by name.
1439
+
1440
+ call: teleport(x, y) # two coordinates
1441
+ --or: teleport(x) # jump to to x position, keeping y as is
1442
+ --or: teleport(y=y) # jump to to y position, keeping x as is
1443
+ --or: teleport(x, y, fill_gap=True)
1444
+ # jump but fill the gap in between
1445
+
1446
+ Move turtle to an absolute position. Unlike goto(x, y), a line will not
1447
+ be drawn. The turtle's orientation does not change. If currently
1448
+ filling, the regions(s) jumped from will be filled after leaving,
1449
+ and filling will begin again after jumping. This can be disabled
1450
+ with fill_gap=True which will do all filling upon the end_fill(). Because the
1451
+ filling is done via SVG commands, this might be different than with Python's teleport
1452
+ function.
1387
1453
  """
1388
1454
  if isinstance(x, tuple) and y is None:
1389
- if len(x) != 2:
1390
- raise ValueError('The tuple argument must be of length 2.')
1391
- y = x[1]
1392
- x = x[0]
1455
+ if len(x) != 2:
1456
+ raise ValueError('The tuple argument must be of length 2.')
1457
+ y = x[1]
1458
+ x = x[0]
1459
+ pendown = self.isdown()
1460
+ was_filling = self.filling()
1461
+ # if pendown:
1462
+ # self.pen(pendown=False)
1463
+ if was_filling and not fill_gap:
1464
+ self.end_fill()
1465
+ if pendown:
1466
+ self.pen(pendown=False)
1467
+ new_x = x if x is not None else self.getx()
1468
+ new_y = y if y is not None else self.gety()
1393
1469
  animate_temp = self.animate
1394
1470
  self.penup()
1395
1471
  self.animationOff()
1396
- self.goto(x,y)
1472
+ self.goto(new_x,new_y)
1397
1473
  self.animate = animate_temp
1398
- self.pendown()
1474
+ self.pen(pendown=pendown)
1475
+ if was_filling and not fill_gap:
1476
+ self.begin_fill()
1477
+ teleport = jumpto #alias
1399
1478
 
1400
1479
  # Move the turtle to a designated 'x' x-coordinate, y-coordinate stays the same
1401
1480
  def setx(self, x):
@@ -1534,7 +1613,8 @@ class RawTurtle:
1534
1613
  return self.turtle_speed
1535
1614
  speeds = {'fastest':13, 'fast':10, 'normal':5, 'slow':3, 'slowest':1}
1536
1615
  if speed in speeds:
1537
- self.turtle_speed = speeds[speed]
1616
+ #self.turtle_speed = speeds[speed]
1617
+ speed = speeds[speed]
1538
1618
  elif not isinstance(speed,(int,float)):
1539
1619
  raise ValueError("speed should be a number between 0 and 13")
1540
1620
  self.turtle_speed = speed
@@ -1772,7 +1852,7 @@ class RawTurtle:
1772
1852
  return 360-deg
1773
1853
 
1774
1854
  def extract_points(self):
1775
- svg_string = shapeDict[self.turtle_shape]
1855
+ svg_string = self.shapeDict[self.turtle_shape]
1776
1856
  match = re.search(r'points="([^"]*)"', svg_string)
1777
1857
  if not match:
1778
1858
  raise ValueError("No points attribute found")
@@ -2580,11 +2660,11 @@ def getcolor(n):
2580
2660
  raise ValueError("color index must be an integer between 0 and 139")
2581
2661
  n = int(round(n))
2582
2662
  if (n < 0) or (n > 139):
2583
- raise valueError("color index must be an integer between 0 and 139")
2663
+ raise ValueError("color index must be an integer between 0 and 139")
2584
2664
  return VALID_COLORS[n]
2585
2665
 
2586
2666
 
2587
- _tg_screen_functions = ['addshape', 'bgcolor', 'clearscreen', 'drawline', 'hideborder',
2667
+ _tg_screen_functions = ['addshape', 'bgcolor', 'clearscreen', 'colormode', 'drawline', 'hideborder',
2588
2668
  'initializescreen','initializeTurtle', 'showSVG', 'saveSVG', 'line', 'mode', 'register_shape', 'resetscreen', 'setup',
2589
2669
  'setworldcoordinates', 'showborder', 'turtles', 'window_width', 'window_height' ]
2590
2670
 
@@ -2597,7 +2677,7 @@ _tg_turtle_functions = ['animationOff', 'animationOn', 'bk', 'back', 'backward',
2597
2677
  'isvisible', 'jumpto', 'left', 'lt', 'pd', 'pen', 'pencolor', 'pensize', 'pendown', 'penup', 'pos',
2598
2678
  'position', 'pu', 'radians', 'regularPolygon', 'reset', 'right', 'rt', 'setheading', 'seth',
2599
2679
  'setpos', 'setposition', 'settiltangle', 'setx','sety', 'shape', 'shapesize', 'shearfactor',
2600
- 'showturtle', 'speed', 'st', 'stamp', 'tilt', 'tiltangle', 'turtlesize', 'towards', 'up', 'update',
2680
+ 'showturtle', 'speed', 'st', 'stamp', 'teleport', 'tilt', 'tiltangle', 'turtlesize', 'towards', 'up', 'update',
2601
2681
  'width', 'write', 'xcor', 'ycor' ]
2602
2682
 
2603
2683
  def _getmethparlist(ob):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ColabTurtlePlus
3
- Version: 2.1.1
3
+ Version: 2.2.0
4
4
  Summary: An HTML based Turtle implementation with classes for Google Colab and Jupyter Labs
5
5
  Home-page: https://github.com/mathriddle/ColabTurtlePlus
6
6
  Author: Larry Riddle
@@ -39,6 +39,10 @@ Sept. 2026: Version 2.1.0 adds a register_shape (alias addshape) function to cre
39
39
 
40
40
  Sept. 2026: Version 2.1.1 allows use of addcomponent() to add a polygon to a component turtle shape. There are two new ways to build additional components: addellipsecomponent() adds an elliptical shape, and addpathcompenent adds a path defined using svg path commands.
41
41
 
42
+ Sept. 2026: Version 2.1.2. Bug fix when multiple turtles have the same shape.
43
+
44
+ Oct. 2026: Version 2.2.0. Added colormode and modified jumpto to mimic teleport from Python's turtle module.
45
+
42
46
  Installation
43
47
  ----
44
48
  Create an empty code cell and type:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ColabTurtlePlus
3
- Version: 2.1.1
3
+ Version: 2.2.0
4
4
  Summary: An HTML based Turtle implementation with classes for Google Colab and Jupyter Labs
5
5
  Home-page: https://github.com/mathriddle/ColabTurtlePlus
6
6
  Author: Larry Riddle
@@ -39,6 +39,10 @@ Sept. 2026: Version 2.1.0 adds a register_shape (alias addshape) function to cre
39
39
 
40
40
  Sept. 2026: Version 2.1.1 allows use of addcomponent() to add a polygon to a component turtle shape. There are two new ways to build additional components: addellipsecomponent() adds an elliptical shape, and addpathcompenent adds a path defined using svg path commands.
41
41
 
42
+ Sept. 2026: Version 2.1.2. Bug fix when multiple turtles have the same shape.
43
+
44
+ Oct. 2026: Version 2.2.0. Added colormode and modified jumpto to mimic teleport from Python's turtle module.
45
+
42
46
  Installation
43
47
  ----
44
48
  Create an empty code cell and type:
@@ -14,6 +14,10 @@ Sept. 2026: Version 2.1.0 adds a register_shape (alias addshape) function to cre
14
14
 
15
15
  Sept. 2026: Version 2.1.1 allows use of addcomponent() to add a polygon to a component turtle shape. There are two new ways to build additional components: addellipsecomponent() adds an elliptical shape, and addpathcompenent adds a path defined using svg path commands.
16
16
 
17
+ Sept. 2026: Version 2.1.2. Bug fix when multiple turtles have the same shape.
18
+
19
+ Oct. 2026: Version 2.2.0. Added colormode and modified jumpto to mimic teleport from Python's turtle module.
20
+
17
21
  Installation
18
22
  ----
19
23
  Create an empty code cell and type:
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = ColabTurtlePlus
3
- version = 2.1.1
3
+ version = 2.2.0
4
4
  author = Larry Riddle
5
5
  author_email = lriddle@agnesscott.edu
6
6
  description = 'An HTML based Turtle implementation with classes for Google Colab and Jupyter Labs'
@@ -9,7 +9,7 @@ README = (HERE / "README.md").read_text()
9
9
 
10
10
  setup(
11
11
  name='ColabTurtlePlus',
12
- version='2.1.1',
12
+ version='2.2.0',
13
13
  packages=['ColabTurtlePlus'],
14
14
  url='https://github.com/mathriddle/ColabTurtlePlus',
15
15
  license='MIT',
File without changes