# ==============================================================================
"""GRID : create some grids with several color patterns"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "2.0" # use multi-state widgets
__date__    = "2022-11-12"
# ==============================================================================
from ezTK import *
from random import randrange
# ------------------------------------------------------------------------------
def stripes(width:int=400, height:int=400, rows:int=40) -> None:
  """alternate black and white horizontal stripes"""
  win = Win(title='STRIPES') # create main window
  height = height // rows # set height for a single stripe
  colors = ('#000','#FFF') # store stripe colors in a tuple
  # ----------------------------------------------------------------------------
  for row in range(rows): # loop over the stripes
    state = row % 2 # set state for new stripe (cycle on 2 states)
    Brick(win, bg=colors, state=state, width=width, height=height)
  # ----------------------------------------------------------------------------
  win.loop() # start interaction loop
# ------------------------------------------------------------------------------
def colorstripes(width:int=400, height:int=400, rows:int=40) -> None:
  """alternate color stripes using six colors"""
  win = Win(title='COLORSTRIPES') # create main window
  height = height // rows # set height for a single stripe
  colors = ('#F00','#0F0','#00F','#0FF','#F0F','#FF0') # store stripe colors
  # ----------------------------------------------------------------------------
  for row in range(rows): # loop over the stripes
    state = row % 6 # set state for new stripe (cycle on 6 states)
    Brick(win, bg=colors, state=state, width=width, height=height)
  # ----------------------------------------------------------------------------
  win.loop() # start interaction loop
# ------------------------------------------------------------------------------
def chessboard(width:int=400, height:int=400, cols:int=8, rows:int=8) -> None:
  """create black and white chessboard"""
  win = Win(title='CHESSBOARD', fold=cols) # set fold property to number of cols
  width, height = width // cols, height // rows # set size for a single cell
  colors = ('#000','#FFF') # store cell colors
  # ----------------------------------------------------------------------------
  for loop in range(rows*cols): # loop over the board cells
    row, col = loop // cols, loop % cols # get cell coords by Euclidian division
    state = (row+col) % 2 # set state for new cell (cycle on 2 states)
    Brick(win, bg=colors, state=state, height=height, width=width)
  # ----------------------------------------------------------------------------
  # Alternative version using nested loops on rows and cols
#  for row in range(rows): # loop over board rows
#    for col in range(cols): # loop over board cols
#      state = (row+col) % 2 # set state for new cell (cycle on 2 states)
#      Brick(win, bg=colors, state=state, height=height, width=width)
  # ----------------------------------------------------------------------------
  win.loop() # start interaction loop
# ------------------------------------------------------------------------------
def colorboard(width:int=400, height:int=400, cols:int=8, rows:int=8) -> None:
  """create six color checkerboard"""
  win = Win(title='COLORBOARD', fold=cols, op=2) # set 2-pixel outer padding
  width, height = width // cols, height // rows # set size for a single cell
  colors = ('#F00','#0F0','#00F','#0FF','#F0F','#FF0') # store cell colors
  # ----------------------------------------------------------------------------
  for loop in range(rows*cols): # loop over the board cells
    row, col = loop // cols, loop % cols # get cell coords by Euclidian division
    state = (row+col) % 6 # set state for new cell (cycle on 6 states)
    Brick(win, bg=colors, state=state, height=height, width=width, border=3)
  # ----------------------------------------------------------------------------
  win.loop() # start interaction loop
# ------------------------------------------------------------------------------
def boxes(width:int=400, height:int=400, cols:int=13, rows:int=13) -> None:
  """create concentric black and white boxes"""
  win = Win(title='BOXES', fold=cols) # set fold property to number of cols
  width, height = width // cols, height // rows # set size for a single cell
  colors = ('#000','#FFF') # store cell colors
  # ----------------------------------------------------------------------------
  for loop in range(rows*cols): # loop over the board cells
    row, col = loop // cols, loop % cols # get cell coords by Euclidian division
    # for each cell, compute its distance to the topmost row, bottommost row,
    # leftmost col and rightmost col, then keep the smallest of the 4 distances
    distance = min(row, rows-row-1, col, cols-col-1)
    state = distance % 2 # set state for new cell (cycle on 2 states)
    Brick(win, bg=colors, state=state, height=height, width=width)
  # ----------------------------------------------------------------------------
  win.loop() # start interaction loop
# ------------------------------------------------------------------------------
def colorboxes(width:int=400, height:int=400, rows:int=13, cols:int=13) -> None:
  """create concentric color boxes"""
  win = Win(title='COLORBOXES', fold=cols) # fold is controlled by number of cols
  width, height = width // cols, height // rows # set size for a single cell
  colors = ('#F00','#0F0','#00F','#0FF','#F0F','#FF0') # store cell colors
  # ----------------------------------------------------------------------------
  for loop in range(rows*cols): # loop over the board cells
    row, col = loop // cols, loop % cols # get cell coords by Euclidian division
    # for each cell, compute its distance to the topmost row, bottommost row,
    # leftmost col and rightmost col, then keep the smallest of the 4 distances
    distance = min(row, rows-row-1, col, cols-col-1)
    state = distance % 6 # set state for new cell (cycle on 6 states)
    Brick(win, bg=colors, state=state, height=height, width=width)
  # ----------------------------------------------------------------------------
  win.loop() # start interaction loop
# ------------------------------------------------------------------------------
def ballboard(width:int=600, height:int=600, cols:int=12, rows:int=12) -> None:
  """create board containing one random color ball for each cell"""
  win = Win(title='BALLBOARD', fold=cols, bg='#000') # set win background as black
  width, height = width // cols, height // rows # set size for a single cell
  images = tuple(Image(f"Z{c}.gif") for c in 'RGBCMY') # store images for cells
  # ----------------------------------------------------------------------------
  for loop in range(rows*cols): # loop over the board cells
    row, col = loop // cols, loop % cols # get cell coords by Euclidian division
    state = randrange(6) # set random state for new cell
    Brick(win, image=images, state=state, height=height, width=width)
  # ----------------------------------------------------------------------------
  win.loop() # start interaction loop
# ==============================================================================
if __name__ == "__main__":
  stripes()
  stripes(800, 600, 200) # do not use default values
  colorstripes()
  chessboard()
  colorboard()
  boxes()
  colorboxes()
  ballboard()
# ==============================================================================
