# ==============================================================================
"""COLORS : edit a color with RGB scales"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "2.0" # add 'RANDOM' and 'PSYCHE' buttons
__date__    = "2022-11-12"
# ==============================================================================
from ezTK import *
from random import randrange as rr # import 'randrange' and rename as 'rr'
# ------------------------------------------------------------------------------
def main():
  """create the main window and pack the widgets"""
  global win
  win = Win(title='COLORS', op=5) # create main window
  # ----------------------------------------------------------------------------
  frame = Frame(win, grow=False) # create frame for top line
  Label(frame, width=9, border=1) # use fixed width to avoid layout jittering
  for rgb in ('#F00', '#0F0', '#00F'): # loop over the 3 primary colors R,G,B
    Scale(frame, scale=(0,255,1), show=False, troughcolor=rgb, command=on_scale)
  Button(frame, text='RANDOM', command=on_random) # create random button
  Button(frame, text='PSYCHE', command=on_psyche) # create psyche button
  Brick(win, height=200) # create brick widget to show selected color
  # ----------------------------------------------------------------------------
  win.brick, win.label, win.switch = win[1], frame[0], frame[5]
  win.R, win.G, win.B = frame[1], frame[2], frame[3]
  on_scale(); win.loop() # set initial color as '#000000' and start user loop
# ------------------------------------------------------------------------------
def on_scale() -> None:
  """callback function for all three RGB scales"""
  # get current value for the three RGB scales
  x, r, g, b = '0123456789ABCDEF', win.R.state, win.G.state, win.B.state
  # convert decimal integer values into hexadecimal color string (= '#RRGGBB')
  color = '#' + x[r//16] + x[r%16] + x[g//16] + x[g%16] + x[b//16] + x[b%16]
  win.brick['bg'] = win.label['text'] = color # set new color on brick and label
# ------------------------------------------------------------------------------
def on_random() -> None:
  """callback function for the RANDOM button"""
  # use rr(256) to select random value between 0 and 255 for R,G,B scales
  win.R.state, win.G.state, win.B.state = rr(256), rr(256), rr(256)
# ------------------------------------------------------------------------------
def on_psyche() -> None:
  """callback function for the PSYCHE button"""
  on_random(); win.after(100, on_psyche) # new random color every 100 ms
# ==============================================================================
if __name__ == '__main__':
  main()
# ==============================================================================
