Showing posts with label Kandinsky. Show all posts
Showing posts with label Kandinsky. Show all posts

Saturday, September 26, 2026

Python in Numworks: Duplicating and Grayscale

Python in Numworks: Duplicating and Grayscale




All three scripts presented today use the math, random, and the Numworks specific



Stage 1: Generating Boxes of Random Colors



The script generates 15 boxes of random colors of 30 pixels each. This the list of available colors:





red: (255, 0, 0)

orange: (255, 127,0)

lime green: (0, 255, 0)

teal: (0, 128, 128)

blue: (0, 0, 255)

yellow: (255, 255, 0)

dark green: (0, 128, 0)

purple: (128, 0, 128)



The kandinsky command fill_rect draws a filled rectangle and has the following syntax:

fill_rect( x, y, width, height, rbg tuple )

(x,y) is the upper left hand corner of the rectangle in pixels (size 320 x 220).


from math import *

from kandinsky import *

from random import *


'''

draw boxes of random colors


Edward Shore

6/7/2026

'''


# set up eight colors

# red,orange,lime,teal,

# blue,yellow,dark green,purple


colors=[(255,0,0),(255,127,0),(0,255,0),(0,128,128),(0,128,0),(128,0,128)]


# draw 15 squares of random colors

for i in range(3):

  for j in range(5):

    col=choice(colors)

    fill_rect(30+30*i,30+30*j,30,30,col)


(results will vary)



Stage 2: Duplicating the Boxes



This script generates 15 boxes and then copies them to another area. I wanted the script to determine the pixel color’s because in more complex programs, we may not always know the color tuple of a pixel in advance.



The command get_pixel returns a color’s tuple:

get_pixel(x, y) → (red, green, blue)



Although this script does not use the command, Kandinsky also has a set_pixel command which turns a pixel a certain color.

set_pixel(x, y, (red, green, blue))


from math import *

from kandinsky import *

from random import *


'''

draw boxes of random colors

and dupicate them

Edward Shore

6/7/2026

'''


# set up eight colors

# red,orange,lime,teal,

# blue,yellow,dark green,purple


colors=[(255,0,0),(255,127,0),(0,255,0),(0,128,128),(0,128,0),(128,0,128)]


# draw 15 squares of random colors

for i in range(3):

  for j in range(5):

    col=choice(colors)

    fill_rect(30+30*i,30+30*j,30,30,col)

    # get the color

    gcol=get_pixel(30+30*i,30+30*j)

    fill_rect(150+30*i,30+30*j,30,30,gcol)


(results will vary)



Stage 3: Grayscale



The third script converts the duplicated pixel to grayscale using this formula:

grayscale = red * 0.299 + green * 0.587 + blue * 0.114

and the resulting color tuple is (grayscale, grayscale, grayscle)



The weights are selected to accommodate the human eye’s sensitivity to red, green, and blue.


from math import *

from kandinsky import *

from random import *


'''

draw boxes of random colors

and dupicate them in grayscale

Edward Shore

6/7/2026

'''


colors=[(255,0,0),(255,127,0),(0,255,0),(0,128,128),(0,128,0),(128,0,128)]


# draw 15 squares of random colors

for i in range(3):

  for j in range(5):

    col=choice(colors)

    fill_rect(30+30*i,30+30*j,30,30,col)

    # get the color

    w=get_pixel(30+30*i,30+30*j)

    # convert to grayscale

    w=w[0]*0.299+w[1]*0.587+w[2]*0.114

    # create a tuple

    gcol=(w,w,w)

    fill_rect(150+30*i,30+30*j,30,30,gcol)


(results will vary)



Source



“Color Space Conversion & Binarization for Image Processing” Dynamsoft. May 24, 2019. https://www.dynamsoft.com/blog/insights/image-processing/image-processing-101-color-space-conversion/ Retrieved June 6, 2026.



Eddie


All original content copyright, © 2011-2026. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Sunday, September 15, 2024

Numworks: Allowing Repeated Calculations in Python

Numworks: Allowing Repeated Calculations in Python



Introduction


Say we want the user to repeat a calculation or a routine for as long as the user wants. At the end of each calculation, the user is asked if they want another calculation. The user enters one of two possibilities:


1 for an additional calculation

0 for no more calculation


A flag variable is set up with a default value of 1. As long as this value remains at 1, the calculation repeats.


Scrip structure (I named this variable rptflag, but it can be any name):


rptflag=1


while rptflag!=0:

  …

  < insert inputs and show results >

  …

  print(“Again? No = 0, Yes = 1”)

  rptflag=eval(input())


Entering 0 for the Again? Question exits the loop. We need the eval (evaluate) or int (input) because leaving the input command alone defaults the input to a string.


For the sample scripts below, the escape velocity off the surface of planets and other celestial objects are calculated.


Repeated Loop: General


This script does not require any special modules.


Script: rptdemo1 (We can use this on any calculator or platform)


from math import *


# repeat demonstration 

# set up repeat flag

rptflag=1


while rptflag!=0:

  print("ESCAPE VELOCITY")

  m=eval(input("Mass (kg)? "))

  r=eval(input("Radius (m)? "))

  v=sqrt(1.33486e-10*m/r)

  print("Velocity: ",v,"\nm/s")

  print("Again? no=0, 1=yes")

  rptflag=eval(input())




Repeated Loop: Numworks – using the keyboard


Instead of having the user enter 0 or 1, the user presses the keys 0 or 1. To allow the user key presses, use the Ion module. KEY_ZERO is for the 0 key and KEY_ONE is for the 1 key.


To make the screen look presentable, I’m clearing the screen. This is accomplished by the use of the Kandinsky module. The use of the Kandinsky module was suggest to me on Reddit by Feeling_Walrus3033, and I give credit and gratitude for the suggestion.


The line:


fill_rect(0,0,320,240,(255,255,255)) creates a blank white screen.


Clearing the screen this way will call for a way to display text on the screen. Unfortunately the print() command won’t do it. Kandinsky comes to the rescue with the draw_string command. The syntax for the draw_string command is:


draw_string(“text” or variable containing the string, x pixel, y pixel, [text color], [background color])


Colors are optional. The default is black text on white background.


Keep in mind that using this method will require more memory.


Script: rptdemo2 (Specific to Numworks, for other platforms and calculator, check your specific manual)


from math import *

from ion import *

from kandinsky import *

# repeat demonstration 


# set up keys

def key():

  while True:

    if keydown(KEY_ZERO):

      return 0

    if keydown(KEY_ONE):

      return 1


# set up repeat flag

rptflag=1


while rptflag==1:

  print("ESCAPE VELOCITY")

  m=eval(input("Mass (kg)? "))

  r=eval(input("Radius (m)? "))

  v=sqrt(1.33486e-10*m/r)

  

  # build strings

  s1="Velocity: "+str(v)+" m/s"

  s2="Again? no=0, 1=yes"

  fill_rect(0,0,320,240,(255,255,255))

  draw_string(s1,0,0)

  draw_string(s2,0,20)

  rptflag=key()


draw_string("DONE",0,40,(255,0,0))



Sample Data to Try



Mass (kg)

Radius (m)

Escape Velocity (m/s)

Earth

5.972168E24

6378.137E3

11179.88618486758

Jupiter

1.8982E27

71492E3

59533.32250562

Sun (Solar System)

1.9885E30

6.957E8

617688.6988877566


E: [ ×10^x ] button, shown as “e”



Note: I will be in Nashville for the 2024 HP Handheld Conference on September 21-22, 2024. The next scheduled post will be on September 28, 2024.


Take care,


Eddie


All original content copyright, © 2011-2024. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.



Monday, August 23, 2021

Nuwmorks: Three Doors Game

Nuwmorks: Three Doors Game


Happy Monday!


Introduction


In the popular game show Let's Make a Deal, there is a final round called the Big Deal.  In the Big Deal, one or two contestants picked from one of three doors.   One door had the "Big Deal" containing the biggest prize.  


This game simulates a modified round of the Big Deal.  You are tasked of choosing one of three doors, and the doors contain:


*  $5,000 (not a real contest, this is for gaming purposes)

*  A car

*  A goat 


Numworks Python Script: thedoorgame.py


https://my.numworks.com/python/ews31415/thedoorgame


Required modules:  math, random, kandinsky, ion


# 2021-08-05 EWS

# Three Door Game

# based on Lets Make A Deal

from math import *

from random import *

from kandinsky import *

from ion import *


# set up the key function

def key():

  while True:

    if keydown(KEY_ONE):

      return 1

    if keydown(KEY_TWO):

      return 2

    if keydown(KEY_THREE):

      return 3


# initial doors: indigo, fir green, orange

fill_rect(40,15,100,75,color(43,43,95))

fill_rect(140,15,200,75,color(34,139,34))

fill_rect(240,15,300,75,color(255,127,0))

# text, wheat

draw_string("1",70,45,color(245,222,179),color(43,43,95))

draw_string("2",170,45,color(245,222,179),color(34,139,34))

draw_string("3",270,45,color(245,222,179),color(255,127,0))

# prize setup

p=["\u0024 5,000","CAR","GOAT"]

l=[0,1,2]

w=[]

for i in range(3):

  a=choice(l)

  w.append(a)

  l.remove(a)

# ask for a door

draw_string("Which door do you want?",40,120,color(0,0,0))

k=key()

if k==1:

  ps=p[w[0]]

if k==2:

  ps=p[w[1]]

if k==3:

  ps=p[w[2]]

# the reveal

fill_rect(40,15,100,75,color(0,0,0))

fill_rect(140,15,200,75,color(0,0,0))

fill_rect(240,15,300,75,color(0,0,0))

# text, wheat

draw_string(p[w[0]],45,45,color(245,222,179),color(0,0,0))

draw_string(p[w[1]],145,45,color(245,222,179),color(0,0,0))

draw_string(p[w[2]],245,45,color(245,222,179),color(0,0,0))

draw_string("You win: "+ps,40,160,color(0,0,0))


Download the Python file here:  https://drive.google.com/file/d/1yR8bBqmFwOeKZqBSgCVnoQkUcqDZzvu6/view?usp=sharing


Good luck!


Eddie


All original content copyright, © 2011-2021.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Sunday, June 28, 2020

Numworks Python Scripts: Basic Graphics

Numworks Python Scripts:  Basic Graphics

Script:  atari.py

Draws the basic-eight color palette of the classic 1977 Atari 2600.

Atari Palette Python Script
Atari Palette Python Script


from math import *
from kandinsky import *
# 2020-05-28 atari 2600 colors
# kandinsky module

fill_rect(0,0,320,240,color(245,245,245))

fill_rect(15,15,55,55,color(0,0,0))
fill_rect(85,15,55,55,color(255,0,0))
fill_rect(155,15,55,55,color(255,255,0))
fill_rect(15,85,55,55,color(255,0,255))
fill_rect(155,85,55,55,color(0,255,0))
fill_rect(15,155,55,55,color(0,255,255))
fill_rect(85,155,55,55,color(0,0,255))
fill_rect(155,155,55,55,color(255,255,255))

draw_string("8",107,107)


Script:  firstdigit.py

The tenths digit from n random numbers is extracted and a bar chart is generated based on the results.  I recommend a sample size of at least 20.

firstdigit script example (sample = 100)
A Sample of 100 data points



from math import *
from random import *
from matplotlib.pyplot import *

# set up lists
x=[0,1,2,3,4,5,6,7,8,9]
y=[0,0,0,0,0,0,0,0,0,0]

# user iput
print("EWS 2020-05-29")
print("Bar Chart: First Digit")
print("Recommended at least 20")
n=int(input("n? "))

# generate list
for i in range(n):
  s=int(random()*10)
  y[s]=y[s]+1

# bar plot
h=int(n/2)
d=-int(h/4)
axis([-0.5,9.5,d,h])
bar(x,y)

# turn axis off
axis("off")

# labels at the bottom
# results at top
m=max(y)
for i in range(10):
  text(i-0.25,d+1,str(i))
  text(i-0.25,m+2,str(y[i]))
  
show()

Script:  colorfulrings.py

The script cycles through a set of nine colors, four times.   The Kandinsky module is used to generate the flowery circles as well as cycle through the colors.  This module works with integer pixels.

Color rings script in progress
Color rings script in progress


from math import *
from kandinsky import *
from time import *

# color lists
r=[255,255,255,0,0,0,51,128,255]
g=[0,102,255,128,255,0,102,128,255]
b=[0,0,0,0,0,255,255,128,255]

# angles
a=list(range(128))
for i in range(128):
  a[i]=i/128*2*pi

# draw circles
for k in range(36):
  n=int(fmod(k,9))
  for j in range(50):
    for i in range(128):
      x=int(160+(20+j)*cos(a[i]))
      y=int(120+(20+j)*sin(a[i]))
      set_pixel(x,y,color(r[n],g[n],b[n]))
  sleep(0.1)

Script:  modulusplot.py

Generate a pixel plot of the equation (x^n + y^n) mod m

Modulus Plot example, n = 3.9, m = 15.6
Input Screen  (n = 3.9, m = 15.6)

Modulus Plot result, n = 3.9, m = 15.6
Modulus Plot result, n = 3.9, m = 15.6


from math import *
from kandinsky import *
print("EWS 2020-05-28")
print("x**n + y**n mod m")
n=float(input("power? "))
m=float(input("modulus? "))

for x in range(320):
  for y in range(240):
    t=fmod(pow(x,n)+pow(y,n),m)  
    c=floor(t/m*255)
    set_pixel(x+1,y+1,color(c,c,c))


Eddie

BLOG UPDATE:
The HP Prime:  Conversion to Binary and IEEE-754 Binary blog entry that was posted on June 27, 2020 may contain errors.  In this case, I have taken that entry back to draft status and my intention is to repost the entry as soon as I can.  Apologies for any inconvenience. 

All original content copyright, © 2011-2020.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Casio fx-50F/Radio Shack EC-4024 Program Collection: October 2026

Casio fx-50F/Radio Shack EC-4024 Program Collection: October 2026 Notes: * The fx-50F/EC-4024 has a 29 step program space betwe...