phy_spectrum.py

Created by antarctus

Created on October 09, 2022

1.02 KB

Permet d’afficher le spectre visible, de sélectionner une couleur et d’afficher sa longueur d’onde. Utilisez les flèches pour choisir la couleur, appuyez sur OK pour affiner la sélection, touche retour pour quitter.

Est issu du programme d’affichage du spectre d’uncurieux.


from math import *
from kandinsky import *
from ion import *
from time import *
from micropython import *

def pos2color(pos):
    r,g,b=255,0,255
    for i in range(pos):
      if i<54:
        r-=int(255/53)
      elif i>85 and i<102:
        g+=int(255/16)
      elif i>101 and i<123:
        b-=int(255/21)
      elif i>170 and i<194:
        r+=int(255/30)
      elif i>194 and i<235:
        g-=int(255/43)
        r=255
      elif i>233:
        g=0
    return r,g,b

def draw_spectrum():
  for i in range(320):
    r,g,b=pos2color(i)
    fill_rect(i,0,1,150,color(r,g,b))


def get_wavelenght(pos):
  return int(pos/320*300+400)

def draw_pos():
  fill_rect(0,150,320,50,(255,)*3)
  fill_rect(pos-1,150,2,50,(0,)*3)


pos=0
draw_spectrum()
kbd_intr(-1)
while not keydown(KEY_BACK):
  draw_pos()
  pos+=(5-4*keydown(KEY_OK))*(keydown(KEY_RIGHT)-keydown(KEY_LEFT))
  pos=min(320,max(0,pos))
  sleep(0.1)
  draw_string("wavelenght : "+str(get_wavelenght(pos))+"nm",5,200)
  fill_rect(190,200,130,22,pos2color(pos))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.