couleur_rgb.py

Created by florian-allard

Created on April 20, 2021

2.17 KB

Petit script qui permet d’afficher une couleur à partir de ses trois composantes Rouge, Verte et Bleue paramétrées par des curseurs. Une fois la couleur choisie, un appui sur la touche OK ou EXE permet de copier le code hexadécimal et le code RGB dans la console et un appui sur la touche Retour (ou Échap) répété deux fois permet d’afficher la console et de voir les codes. On peut choisir et valider plusieurs couleurs avant d’afficher la console.


from kandinsky import *
from ion import *
from time import sleep
x = 130
y = 50
r = g = b = 129
r1 = g1 = b1 = 128

pos = 0
chgt = True
for i in range(3):
  draw_string(str([r1,g1,b1][i]),x+15,y+18*i,"white",["red","green","blue"][i])
    
while 1:
  if chgt or (r1,g1,b1) != (r,g,b):
    chgt = False
    for i in range(3):
      draw_string("< ",x-5,y+18*i,[(r1,g1,b1),["white","black"][r1+g1+b1>384]][pos == i],(r1,g1,b1))
      draw_string(" >",x+45,y+18*i,[(r1,g1,b1),["white","black"][r1+g1+b1>384]][pos == i],(r1,g1,b1))

  if (r1,g1,b1) != (r,g,b):
    r,g,b=r1,g1,b1
    draw_string("   ",x+15,y+18*pos,"white",["red","green","blue"][pos])
    draw_string(str([r,g,b][pos]),x+15+5*(3-len(str([r,g,b][pos]))),y+18*pos,"white",["red","green","blue"][pos])
    fill_rect(0,0,320,50,(r,g,b))
    fill_rect(0,50,x-5,110,(r,g,b))
    fill_rect(x+65,50,320-(x+65),110,(r,g,b))
    fill_rect(x-5,y+54,70,y+85-104,(r,g,b))
    fill_rect(x-5,y+103,70,7,(r,g,b))
    fill_rect(0,y+110,x-5-5*len(str(r)+str(g)+str(b)),222-(y+110),(r,g,b))
    fill_rect(x+65+5*len(str(r)+str(g)+str(b)),y+110,320-(x+65)-5*len(str(r)+str(g)+str(b)),222-(y+110),(r,g,b))
    fill_rect(x-5-5*len(str(r)+str(g)+str(b)),y+128,70+10*len(str(r)+str(g)+str(b)),222-(y+128),(r,g,b))
    draw_string("#"+"0"*(2-len(hex(r)[2:]))+hex(r)[2:]+"0"*(2-len(hex(g)[2:]))+hex(g)[2:]+"0"*(2-len(hex(b)[2:]))+hex(b)[2:],x-5,y+85)
    draw_string("rgb("+str(r)+","+str(g)+","+str(b)+")",x-5-5*len(str(r)+str(g)+str(b)),y+110)
     
  if pos==0:
    r1 = (r+keydown(KEY_RIGHT)-keydown(KEY_LEFT))%256
  elif pos==1:
    g1 = (g+keydown(KEY_RIGHT)-keydown(KEY_LEFT))%256
  else:
    b1 = (b+keydown(KEY_RIGHT)-keydown(KEY_LEFT))%256
  #[r,g,b][pos] += keydown(KEY_RIGHT)-keydown(KEY_LEFT)
  
  if keydown(KEY_EXE) or keydown(KEY_OK):
    print("#"+"0"*(2-len(hex(r)[2:]))+hex(r)[2:]+"0"*(2-len(hex(g)[2:]))+hex(g)[2:]+"0"*(2-len(hex(b)[2:]))+hex(b)[2:])
    print("rgb("+str(r)+","+str(g)+","+str(b)+")")
    draw_string("Affiché dans la console",45,y+130)
    draw_string("Touche Retour pour voir",45,y+150)

  if keydown(KEY_UP) or keydown(KEY_DOWN):
    pos = (pos-keydown(KEY_UP)+keydown(KEY_DOWN))%3
    chgt = True
    sleep(0.05)
  sleep(0.07)

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.