# ==============================================================================
"""COLORS : edit a color with RGB scales"""
# ==============================================================================
__author__  = "Christophe Schlick by Philippe Blasi"
__version__ = "0.0" # skeleton version
__date__    = "2022-11-12"
# ==============================================================================
from ezTK import *
# ------------------------------------------------------------------------------
def main():
  """create the main window and pack the widgets"""
  global win
  win = Win(title='COLORS', op=5)
  # ----------------------------------------------------------------------------
  # TODO (use 1 Frame, 1 Label, 3 Scales, 1 Brick)
  # ----------------------------------------------------------------------------
  frame = Frame(win, op=0, grow=False)
  Label(frame, text='#000000', width=9, border=1, grow=False)
  
  #Scale(frame, scale=(0,255), state=0, show=False, troughcolor='#F00', command=on_scale)
  #Scale(frame, scale=(0,255), state=0, show=False, troughcolor='#0F0', command=on_scale)
  #Scale(frame, scale=(0,255), state=0, show=False, troughcolor='#00F', command=on_scale)

  for color in ('#F00','#0F0','#00F'):
    Scale(frame, scale=(0,255), state=0, show=False, troughcolor=color, command=on_scale)
  
  Brick(win,bg='#000000',height=200,border=1)
  # ----------------------------------------------------------------------------  
  win.brick, win.label = win[1], frame[0]            # friendly names for all
  win.R, win.G, win.B = frame[1],frame[2],frame[3]   # widgets used in callbacks
  win.loop()
# ------------------------------------------------------------------------------
def on_scale() -> None:
  """callback function for all three RGB scales"""
  # TODO (get current values for R,G,B scales and convert them to hexadecimal)
  # TODO (built color string '#RRGGBB' and update properties on Label and Brick)
  hexa = '0123456789ABCDEF'

  r = win.R.state
  g = win.G.state
  b = win.B.state

  r1,r2 = divmod(r,16)
  g1,g2 = divmod(g,16)
  b1,b2 = divmod(b,16)

  color = '#' + hexa[r1] + hexa[r2] + hexa[g1] + hexa[g2] + hexa[b1] + hexa[b2]
  
  win.brick['bg'] =  color
  win.label['text'] = color
  
# ==============================================================================
if __name__ == '__main__':
  main()
# ==============================================================================
