hex_rgb_cololr_decoder.py

Created by wperez274

Created on May 02, 2025

6.57 KB


from math import *
from random import *
from random import randint as RAND
from kandinsky import *
from kandinsky import fill_rect as FILL
from kandinsky import draw_string as STR
from ion import *
from time import *



SW=322
SH=222

red=(255,0,0)
green=(0,255,0)
blue=(0,0,255)

yellow=(255,255,0)
magenta=(255,0,255)
cyan=(0,255,255)

black=(0,)*3
grey=(155,)*3
white=(255,)*3



r=RAND(0,255)
g=RAND(0,255)
b=RAND(0,255)

"""
print("**********************************")
print("\nEnter a number between 0 - 255\n")
try:
  r=int(input("\n\nRed:  "))
except:
  r=RAND(0,255)


try:
  g=int(input("\n\nGreen:  "))
except:
  g=RAND(0,255)


try:
  b=int(input("\n\nBlue:  "))
except:
  b=RAND(0,255)

"""

r_hex=hex(r)[1:]
g_hex=hex(g)[1:]
b_hex=hex(b)[1:]

sleep(0.15)

FILL(0,0,SW,SH,(r,g,b))
FILL(0,50,SW,100,(0,0,0))


def redraw():
  global SW,SH,r,g,b  
  FILL(0,0,SW,SH,(r,g,b))
  FILL(0,60,SW,80,(r,g,b))
  FILL(4,60,125,80,(255,)*3)
  FILL(0,50,SW,100,(0,0,0))

  FILL(0,0,SW,50,(0,)*3)



captured_colors=[]

#main process
while not keydown(KEY_EXE):

  if r<=15:
    r_hex="0"+hex(r)[-1:]
  else:
    r_hex=hex(r)[-2:]


  if g<=15:
    g_hex="0"+hex(g)[-1:]
  else:
    g_hex=hex(g)[-2:]

  if b<=15:
    b_hex="0"+hex(b)[-1:]
  else:
    b_hex=hex(b)[-2:]



  draw_string("Red   = "+str(r),10,60,white,(115,0,0))
  draw_string("Green = "+str(g),10,80,white,(0,115,0))
  draw_string("Blue  = "+str(b),10,100,white,(0,0,115))



  if keydown(KEY_SEVEN):
    sleep(0.02)
    redraw()
    r-=1
    if r<0:
      r=0

  if keydown(KEY_EIGHT):
    sleep(0.02)
    r+=1
    if r>255:
      r=255

    redraw()


  if keydown(KEY_FOUR):
    sleep(0.02)
    g-=1
    if g<0:
      g=0
    redraw()

  if keydown(KEY_FIVE):
    sleep(0.02)
    g+=1
    if g>255:
      g=255

    redraw()

  if keydown(KEY_ONE):
    sleep(0.02)
    b-=1
    if b<0:
      b=0
    redraw()

  if keydown(KEY_TWO):
    sleep(0.02)
    b+=1
    if b>255:
      b=255

    redraw()



  if keydown(KEY_LEFT):
    sleep(0.1)
    r-=1
    if r<0:
      r=0
    g-=1
    if g<0:
      g=0
    b-=1
    if b<0:
      b=0
    redraw()



  if keydown(KEY_RIGHT):
    sleep(0.1)
    r+=1
    if r>255:
      r=255
    g+=1
    if g>255:
      g=255
    b+=1
    if b>255:
      b=255

    redraw()
  




  if keydown(KEY_LEFTPARENTHESIS):
    sleep(0.2)
    r-=1
    if r<0:
      r=0

    redraw()

    if keydown(KEY_SHIFT):
      sleep(0.1)
      r-=5

  if keydown(KEY_RIGHTPARENTHESIS):
    sleep(0.2)
    r+=1
    if r>255:
      r=255

    redraw()
    if keydown(KEY_SHIFT):
      sleep(0.1)
      r+=5
      if r>255:
        r=255

  if keydown(KEY_MULTIPLICATION):
    sleep(0.2)
    g-=1
    if g<0:
      g=0

    redraw()
    if keydown(KEY_SHIFT):
      sleep(0.1)
      g-=5
      if g<0:
        g=0

  if keydown(KEY_DIVISION):
    sleep(0.2)
    g+=1
    if g>255:
      g=255

    redraw()

    if keydown(KEY_SHIFT):
      sleep(0.1)
      g+=5
      if g>255:
        g=255


  if keydown(KEY_PLUS):
    sleep(0.2)
    b-=1
    if b<0:
      b=0

    redraw()

    if keydown(KEY_SHIFT):
      sleep(0.1)
      b-=5
      if b<0:
        b=0


  if keydown(KEY_MINUS):
    sleep(0.2)
    b+=1
    if b>255:
      b=255

    redraw()

    if keydown(KEY_SHIFT):
      sleep(0.1)
      b+=5
      if b>255:
        b=255


  if keydown(KEY_BACKSPACE):
    sleep(0.2)
    r=255
    redraw()
    

  if keydown(KEY_TOOLBOX):
    sleep(0.2)
    r=0
    redraw()


  if keydown(KEY_COMMA):
    sleep(0.2)
    g=0
    redraw()


  if keydown(KEY_POWER):
    sleep(0.2)
    g=255
    redraw()

  if keydown(KEY_SQRT):
    sleep(0.2)
    b=0
    redraw()


  if keydown(KEY_SQUARE):
    sleep(0.2)
    b=255
    redraw()



  if keydown(KEY_OK):
    sleep(0.2)
    r=RAND(0,255)
    g=RAND(0,255)
    b=RAND(0,255)
    
    redraw()



  if r<0: r=0
  if r>255: r=255

  if g<0: g=0
  if g>255: g=255

  if b<0: b=0
  if b>255: b=255





  if keydown(KEY_NINE):
    sleep(0.15)
    try:
      r=int(input("\n\nRed: "))
      if r<0:
        r=0
      if r>255:
        r=255
    except:
      r=0
    sleep(0.2)
      
    redraw()

      
      
  if keydown(KEY_SIX):
    sleep(0.15)
    try:
      g=int(input("\n\nGreen: "))
      if g<0:
        g=0
      if g>255:
        g=255

    except:
      g=0
    sleep(0.2)

    redraw()
    

  if keydown(KEY_THREE):
    sleep(0.15)
    try:
      b=int(input("\n\nBlue: "))
      if b<0:
        b=0
      if b>255:
        b=255

    except:
      b=0
    sleep(0.2)
    redraw()



  draw_string("("+str(r)+","+str(g)+","+str(b)+")",5,130,(255,)*3,(0,)*3)
  draw_string('HEX: '+'"#'+str(r_hex)+str(g_hex)+str(b_hex)+'"',170,140,cyan,black)
  draw_string("Use [(][)][*][/][+],[1 to 9]",1,1,(255,)*3,(0,)*3)
  draw_string("[OK] = Random",2,25,(100,255,255),(0,)*3)
  draw_string("[EXE] = Print",175,25,(150,150,50),(0,)*3)



  if g>0 or b>0:
    red_percent=(r/(r+g+b))*100
    draw_string("r = "+str(round(red_percent))+" %",230,70)
    if red_percent>33:
      draw_string("r = "+str(round(red_percent))+" %",230,70,black,yellow)
    
      

  if r>0 or b>0:
    green_percent=(g/(r+g+b))*100
    draw_string("g = "+str(round(green_percent))+" %",230,90)
    if green_percent>33:
      draw_string("g = "+str(round(green_percent))+" %",230,90,black,yellow)


  if r>0 or g>0:
    blue_percent=(b/(r+g+b))*100
    draw_string("b = "+str(round(blue_percent))+" %",230,110)
    if blue_percent>33:
      draw_string("b = "+str(round(blue_percent))+" %",230,110,black,yellow)

  
  if len(captured_colors)<10:
    if keydown(KEY_ZERO):
      sleep(0.2)
      captured_colors.append((r,g,b))
      STR("color["+str(len(captured_colors))+"] captured!",140,50,(0,0,0),(255,255,100))
      sleep(1)
      STR("color["+str(len(captured_colors))+"] captured!",140,50,(r,g,b),(r,g,b))

      
  FILL(150,70,50,50,(r,g,b))
  FILL(150,70,2,50,white)




  if keydown(KEY_SINE):
    sleep(0.2)
    r=RAND(0,255)
    redraw()
      
  if keydown(KEY_COSINE):
    sleep(0.2)
    g=RAND(0,255)
    redraw()

  if keydown(KEY_TANGENT):
    sleep(0.2)
    b=RAND(0,255)
    redraw()


  if keydown(KEY_PI):
    sleep(0.2)
    r=RAND(0,255)
    g=RAND(0,255)
    b=RAND(0,255)
    
    redraw()



FILL(0,0,320,220,(r,g,b))

sleep(1)

try:
  color_name=input("\nname: ")
except:
  color_name="new_color"


print("\n************************\n")

  

print(color_name," = ",(r,g,b))

hex_code=('"#',r_hex,g_hex,b_hex,'"')

new_hex_code=""


for i in range(len(hex_code)):
  if hex_code[i] != " ":
    new_hex_code += hex_code[i]



print("\nHEX:",new_hex_code)

print("\n************************")

if len(captured_colors)>0:
  print("captured_colors=",captured_colors)


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.