colors.py

Created by ilyas-r

Created on February 14, 2023

6.41 KB

Colors v1.1.
Python Util for Numworks, all models.
By Ilyas R. & Vincent ROBERT. June 2022.

Learn more about Colors on: nsi.xyz/colors (FR)

Changelog

Colors v1.0 - 10/06/2022:
- Initial util

Colors v1.1 - 19/09/2022:
- The [Ans] key is now used to exit the program cleanly


from kandinsky import fill_rect as rec, draw_string as txt
from ion import keydown

# Colors 1.1 NumWorks, 19.09.2022
# Par Ilyas R. & Vincent ROBERT
# https://nsi.xyz/colors <3

bar = 1
rgb = [148,113,222]
black,grey,red,green,blue = ((42,)*3),(184,188,184),(255,0,0),(0,255,0),(0,0,255)
rgb_t = (red,green,blue)
chrs,numpad = (234063,252783,234057,252783,186351,248271,66706),(30,31,32,36,37,38,42,43,44,48)
cuts = (42,0,20,54,86,132,177,210,242,255)


def rgb_to_hex(clr):
    return ("{:0>2}".format(hex(clr[0])[2:])+"{:0>2}".format(hex(clr[1])[2:])+"{:0>2}".format(
            hex(clr[2])[2:])).upper()


def gui():
    clrs=((42*3,42/3,42*5),(42*2,42/7,42*4),(42,42*2,42*5),(42/2,42*5,42*2),
            (42*6,42*5,42),(42*6,42*3,42),(42*6,42,42))
    for k in range(7):
        for j in range(19):
            if chrs[k]>>j&1==1:
                rec(24+12*k+(j%3)*3,10+(j//3)*3,3,3,clrs[k])
    txt("24 bits",28,50,black)
    for n in range(3):
        rec(28,115+(n+1)*20,255,6,grey)
        rec(28,115+(n+1)*20,rgb[n],6,(255*(n+1==bar),42,42))
        rec(22+rgb[n],112+(n+1)*20,12,12,(255*(n+1==bar),42,42))
    txt("R   G   B", 209,5)
    rgb_txt=("R:","G:","B:")
    for i in range(3):
        txt(str(rgb_txt[i]),3,109+(i+1)*20,black)
    for i in range(9):
        txt(str(i+1),12+(33*i)+12*(i==4)+24*(i>4),103,black)
    update_info_display()


def contrast(clr):
    lum=(299*clr[0]+587*clr[1]+114*clr[2])/1000
    if lum>125:
        return black
    else:
        return "white"


def update_info_display():
    for i in range(3):
        txt(str("%03d"%rgb[i]),288,109+20*(i+1),black)
    txt("("+str("%03d"%rgb[0])+","+str("%03d" % rgb[1])+","+str("%03d"%rgb[2])+")",189,30)
    txt("#"+str(rgb_to_hex(rgb)),220,55)
    rec(132,0,56,100,rgb)
    for i in range(4):
        rec(0+33*i,80,33,20,(rgb[0]+int((255-rgb[0])*(4-i)/5),
            rgb[1]+int((255-rgb[1])*(4-i)/5),rgb[2]+int((255-rgb[2])*(4-i)/5)))
    for i in range(4):
        rec(188+33*i,80,33,20,(rgb[0]*(4-i)//5,rgb[1]*(4-i)//5,rgb[2]*(4-i)//5))
    rec(0,200,320,22,rgb)
    txt("Colored by nsi.xyz/colors",33,202,contrast(rgb),rgb)


def number_key(k):
    return 7*(k==30)+8*(k==31)+9*(k==32)+4*(k==36)+5*(k==37)+6*(k==38)+1*(
            k==42)+2*(k==43)+3*(k==44)+0*(k==48)


def sliding_bars_updater(old):
    if not old:
        for i in range(3):
            rec(28,115+(i+1)*20,255,6,grey)
            rec(rgb[i]+22,112+(i+1)*20,12,12,rgb_t[i])
            rec(28,115+(i+1)*20,rgb[i],6,rgb_t[i])
    else:
        for i in range(3):
            rec(rgb[i]+22,112+(i+1)*20,12,12,"white")


def selected_sliding_bar(selected):
    rec(28,115+bar*20,255,6,grey)
    color_s=rgb_t[bar-1] if selected else black
    rec(28,115+bar*20,rgb[bar-1],6,color_s)
    rec(22+rgb[bar-1],112+bar*20,12,12,color_s)


def keyboard_listener(current):
    for i in numpad:
        if keydown(i):
            return i
    return current


def keyboard_shade_listener(num):
    if num==5:
        return rgb
    elif num<5:
        return [rgb[0]+int((255-rgb[0])*(5-num)/5),rgb[1]+int((255-rgb[1])*(5-num)/5),
                rgb[2]+int((255-rgb[2])*(5-num)/5)]
    else:
        return [rgb[0]*(10-num)//5,rgb[1]*(10-num)//5,rgb[2]*(10-num)//5]


def update_slide_bar(old, bar_type, bar_color, cursor):
    rec(old+19,112+bar_type*20,18,12,"white")
    rec(28,115+bar_type*20,255,6,grey)
    rec(28,115 + bar_type * 20, cursor, 6, bar_color)
    rec(cursor + 22, 112 + bar_type * 20, 12, 12, bar_color)


def slide_bar_interaction(bar_type, bar_color, cursor):
    last_key, interaction = None, None
    key_sum, num, old = 0, 120, 0
    while not (keydown(1) or keydown(2)):
        num = keyboard_listener(num)
        if keydown(3) and cursor < 255:
            if key_sum == 0 or key_sum == 30 or key_sum > 60:
                old = cursor
                cursor += 1
                interaction = True
            key_sum += 1
        if keydown(0) and cursor > 0:
            if key_sum == 0 or key_sum == -30 or key_sum < -60:
                old = cursor
                cursor -= 1
                interaction = True
            key_sum -= 1
        if keydown(num) and last_key != num:
            old = cursor
            cursor = cuts[number_key(num)]
            interaction = True
            last_key = num
        if interaction:
            update_slide_bar(old, bar_type, bar_color, cursor)
            rgb[bar_type - 1] = cursor
            update_info_display()
            interaction = False
        if not (keydown(0) or keydown(3) or keydown(num)):
            key_sum, num = 0, 120
            last_key = None


def menu():
    global rgb
    num, old = 120, 0
    last_key, interaction = None, None
    rec(154, 84, 12, 12, contrast(rgb))
    while not keydown(2):
        num = keyboard_listener(num)
        if keydown(3) and last_key != 3:
            old = rgb
            rgb = [rgb[0] * 4 // 5, rgb[1] * 4 // 5, rgb[2] * 4 // 5]
            interaction = True
            last_key = 3
        if keydown(0) and last_key != 0:
            old = rgb
            rgb = [rgb[0] + int((255 - rgb[0]) / 5), rgb[1] + int((255 - rgb[1]) / 5), rgb[2] + int((255 - rgb[2]) / 5)]
            interaction = True
            last_key = 0
        if keydown(num) and last_key != num:
            old = rgb
            rgb = keyboard_shade_listener(number_key(num))
            interaction = True
            last_key = num
        if interaction:
            for i in range(3):
                update_slide_bar(old[i], i + 1, black, rgb[i])
            update_info_display()
            rec(154, 84, 12, 12, contrast(rgb))
            interaction = False
        if not (keydown(0) or keydown(3) or keydown(num)):
            num = 120
            last_key = None


def colors():
    global bar
    last_key, num = None, 42
    gui()
    while not keydown(51):
        if keydown(2) and bar < 3 and last_key != 2:
            if bar != 0:
                selected_sliding_bar(0)
            bar += 1
            selected_sliding_bar(1)
            last_key = 2
        if keydown(1) and bar > 0 and last_key != 1:
            selected_sliding_bar(0)
            bar -= 1
            if bar != 0:
                selected_sliding_bar(1)
            last_key = 1
        if keydown(3) or keydown(0) or keydown(keyboard_listener(num)):
            slide_bar_interaction(bar, rgb_t[bar - 1], rgb[bar - 1])
        if bar == 0:
            menu()
            update_info_display()
        if not (keydown(1) or keydown(2)):
            last_key = None


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.