ColabTurtlePlus 2.1.2__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.
@@ -42,6 +42,11 @@ combined using register_shape().
42
42
 
43
43
  v2.1.2 September 2026
44
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
45
50
  """
46
51
 
47
52
  DEFAULT_WINDOW_SIZE = (800, 600)
@@ -81,6 +86,7 @@ VALID_MODES = ('standard','logo','world','svg')
81
86
  DEFAULT_TURTLE_SHAPE = 'classic'
82
87
  VALID_TURTLE_SHAPES = {'turtle', 'ring', 'classic', 'arrow', 'square', 'triangle', 'circle', 'turtle2', 'blank', 'user'}
83
88
  DEFAULT_MODE = 'standard'
89
+ DEFAULT_COLORMODE = 255
84
90
  DEFAULT_ANGLE_MODE = 'degrees'
85
91
  DEFAULT_POINTS = '-5,-4.5 0,-2.5 5,-4.5 0,4.5'
86
92
  DEFAULT_NAME = 'classic'
@@ -123,7 +129,6 @@ TURTLE_TURTLE2_SVG_TEMPLATE = """<g id="turtle2" visibility="{visibility}" trans
123
129
  TURTLE_USER_SVG_TEMPLATE = """<g id="{id}" visibility="{visibility}" transform="rotate({degrees},{rotation_x},{rotation_y}) translate({turtle_x}, {turtle_y})">
124
130
  <polygon points="{points}" transform="skewX({sk}) scale({sx},{sy})" style="stroke:{pcolor};fill:{turtle_color};stroke-width:{pw}" />
125
131
  </g>"""
126
-
127
132
  TURTLE_COMPONENT_SVG_TEMPLATE = """<g id="user" visibility="{visibility}" transform="rotate({degrees},{rotation_x},{rotation_y}) translate({turtle_x}, {turtle_y})">
128
133
  "{component}"
129
134
  </g>"""
@@ -163,6 +168,7 @@ class _Screen:
163
168
  self._turtles = []
164
169
  self.window_size = DEFAULT_WINDOW_SIZE
165
170
  self._mode = DEFAULT_MODE
171
+ self._colormode = DEFAULT_COLORMODE
166
172
  if self._mode in ['standard','logo']:
167
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
168
174
  self.xscale = self.yscale = 1
@@ -500,7 +506,10 @@ class _Screen:
500
506
  pcolor=turtle.pen_color,
501
507
  pwidth=turtle.pen_width)
502
508
  if turtle.is_filling:
503
- 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])
504
513
  turtle.turtle_pos = new_pos
505
514
  turtle.timeout = timeout_orig
506
515
  if not turtle.animate: self._updateDrawing(turtle=turtle)
@@ -770,6 +779,26 @@ class _Screen:
770
779
  self.xscale = self.yscale = 1
771
780
  self.resetscreen()
772
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
+
773
802
  # Set up user-defined coordinate system using lower left and upper right corners.
774
803
  # Screen is reset.
775
804
  # if the xscale and yscale are not equal, the aspect ratio of the axes and the
@@ -844,33 +873,54 @@ class _Screen:
844
873
  return True
845
874
  return False
846
875
 
847
- # 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
848
877
  def _validateColorTuple(self, color):
849
878
  if len(color) != 3:
850
879
  return False
851
- if not isinstance(color[0], int) or not isinstance(color[1], int) or not isinstance(color[2], int):
852
- return False
853
- if not 0 <= color[0] <= 255 or not 0 <= color[1] <= 255 or not 0 <= color[2] <= 255:
854
- return False
855
- 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
856
892
 
857
- # Helps validate color input to functions
858
- def _processColor(self,color):
893
+ # Helps validate color input to functions
894
+ def _processColor(self, color):
859
895
  if isinstance(color, str):
860
- if color == "": color = "none"
896
+ if color == "":
897
+ color = "none"
861
898
  color = color.lower().strip()
862
- if 'rgb' not in color: color = color.replace(" ","")
899
+ if 'rgb' not in color:
900
+ color = color.replace(" ", "")
863
901
  if not self._validateColorString(color):
864
- err = 'Color ' + color + ' is invalid. It can be a known html color name, 3-6 digit hex string, or rgb string.'
865
- raise ValueError(err)
866
- 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
867
907
  elif isinstance(color, tuple):
868
908
  if not self._validateColorTuple(color):
869
- 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}]'
870
916
  raise ValueError(err)
871
- 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])) + ')'
872
921
  else:
873
- 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'
874
924
  raise ValueError(err)
875
925
 
876
926
  #----------------------------------------------------------------------------------------------
@@ -978,8 +1028,7 @@ class Shape(object):
978
1028
  #-------------------------------------------------------------
979
1029
 
980
1030
  #----------------------------------------------------------------------------------------------
981
-
982
-
1031
+
983
1032
  class RawTurtle:
984
1033
 
985
1034
  def __init__(self, window=None):
@@ -1380,26 +1429,52 @@ class RawTurtle:
1380
1429
  setposition = goto # alias
1381
1430
 
1382
1431
  # jump to a point without drawing or animation
1383
- def jumpto(self,x,y=None):
1384
- """Jumps to a specified point without drawing/animation
1385
-
1386
- Args:
1387
- x: a number or a pair of numbers
1388
- y: a number or None
1389
-
1390
- 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.
1391
1453
  """
1392
1454
  if isinstance(x, tuple) and y is None:
1393
- if len(x) != 2:
1394
- raise ValueError('The tuple argument must be of length 2.')
1395
- y = x[1]
1396
- 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()
1397
1469
  animate_temp = self.animate
1398
1470
  self.penup()
1399
1471
  self.animationOff()
1400
- self.goto(x,y)
1472
+ self.goto(new_x,new_y)
1401
1473
  self.animate = animate_temp
1402
- self.pendown()
1474
+ self.pen(pendown=pendown)
1475
+ if was_filling and not fill_gap:
1476
+ self.begin_fill()
1477
+ teleport = jumpto #alias
1403
1478
 
1404
1479
  # Move the turtle to a designated 'x' x-coordinate, y-coordinate stays the same
1405
1480
  def setx(self, x):
@@ -1538,7 +1613,8 @@ class RawTurtle:
1538
1613
  return self.turtle_speed
1539
1614
  speeds = {'fastest':13, 'fast':10, 'normal':5, 'slow':3, 'slowest':1}
1540
1615
  if speed in speeds:
1541
- self.turtle_speed = speeds[speed]
1616
+ #self.turtle_speed = speeds[speed]
1617
+ speed = speeds[speed]
1542
1618
  elif not isinstance(speed,(int,float)):
1543
1619
  raise ValueError("speed should be a number between 0 and 13")
1544
1620
  self.turtle_speed = speed
@@ -2584,11 +2660,11 @@ def getcolor(n):
2584
2660
  raise ValueError("color index must be an integer between 0 and 139")
2585
2661
  n = int(round(n))
2586
2662
  if (n < 0) or (n > 139):
2587
- 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")
2588
2664
  return VALID_COLORS[n]
2589
2665
 
2590
2666
 
2591
- _tg_screen_functions = ['addshape', 'bgcolor', 'clearscreen', 'drawline', 'hideborder',
2667
+ _tg_screen_functions = ['addshape', 'bgcolor', 'clearscreen', 'colormode', 'drawline', 'hideborder',
2592
2668
  'initializescreen','initializeTurtle', 'showSVG', 'saveSVG', 'line', 'mode', 'register_shape', 'resetscreen', 'setup',
2593
2669
  'setworldcoordinates', 'showborder', 'turtles', 'window_width', 'window_height' ]
2594
2670
 
@@ -2601,7 +2677,7 @@ _tg_turtle_functions = ['animationOff', 'animationOn', 'bk', 'back', 'backward',
2601
2677
  'isvisible', 'jumpto', 'left', 'lt', 'pd', 'pen', 'pencolor', 'pensize', 'pendown', 'penup', 'pos',
2602
2678
  'position', 'pu', 'radians', 'regularPolygon', 'reset', 'right', 'rt', 'setheading', 'seth',
2603
2679
  'setpos', 'setposition', 'settiltangle', 'setx','sety', 'shape', 'shapesize', 'shearfactor',
2604
- 'showturtle', 'speed', 'st', 'stamp', 'tilt', 'tiltangle', 'turtlesize', 'towards', 'up', 'update',
2680
+ 'showturtle', 'speed', 'st', 'stamp', 'teleport', 'tilt', 'tiltangle', 'turtlesize', 'towards', 'up', 'update',
2605
2681
  'width', 'write', 'xcor', 'ycor' ]
2606
2682
 
2607
2683
  def _getmethparlist(ob):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ColabTurtlePlus
3
- Version: 2.1.2
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
@@ -41,6 +41,8 @@ Sept. 2026: Version 2.1.1 allows use of addcomponent() to add a polygon to a com
41
41
 
42
42
  Sept. 2026: Version 2.1.2. Bug fix when multiple turtles have the same shape.
43
43
 
44
+ Oct. 2026: Version 2.2.0. Added colormode and modified jumpto to mimic teleport from Python's turtle module.
45
+
44
46
  Installation
45
47
  ----
46
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.2
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
@@ -41,6 +41,8 @@ Sept. 2026: Version 2.1.1 allows use of addcomponent() to add a polygon to a com
41
41
 
42
42
  Sept. 2026: Version 2.1.2. Bug fix when multiple turtles have the same shape.
43
43
 
44
+ Oct. 2026: Version 2.2.0. Added colormode and modified jumpto to mimic teleport from Python's turtle module.
45
+
44
46
  Installation
45
47
  ----
46
48
  Create an empty code cell and type:
@@ -16,6 +16,8 @@ Sept. 2026: Version 2.1.1 allows use of addcomponent() to add a polygon to a com
16
16
 
17
17
  Sept. 2026: Version 2.1.2. Bug fix when multiple turtles have the same shape.
18
18
 
19
+ Oct. 2026: Version 2.2.0. Added colormode and modified jumpto to mimic teleport from Python's turtle module.
20
+
19
21
  Installation
20
22
  ----
21
23
  Create an empty code cell and type:
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = ColabTurtlePlus
3
- version = 2.1.2
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.2',
12
+ version='2.2.0',
13
13
  packages=['ColabTurtlePlus'],
14
14
  url='https://github.com/mathriddle/ColabTurtlePlus',
15
15
  license='MIT',
File without changes