from casioplot import *
from math import cos,sin,pi


def RGBtoHSL(R,G,B):
  R = R/255
  G = G/255
  B = B/255
  xmin = min(R,G,B)
  xmax = max(R,G,B)
  L = (xmax+xmin)/2
  
  if xmax == xmin:
    S = 0
    H = 0
  elif L <= 1/2:
    S = (xmax-xmin)/(xmax+xmin)
  elif L > 1/2:
    S = (xmax-xmin)/(2-xmax-xmin)
  
  if R == xmax:
    H = (G-B)/(xmax-xmin)
  elif G == xmax:
    H = 2+(R-B)/(xmax-xmin)
  elif B == xmax:
    H = 4+(R-G)/(xmax-xmin)
  
  H = H*60
  if H < 0:
    H += 360
  
  H = round(H)
  S = round(S*100)
  L = round(L*100)
  
  return H,S,L

def HSLtoRGB(H,S,L):
  L = L/100
  
  if S == 0:
    R = round(L*255)
    G = round(L*255)
    B = round(L*255)
    
    return R,G,B
  
  if L < 0.5:
    temp_1 = L*(1+(S/100))
  elif L >= 0.5:
    temp_1 = L+(S/100)-L*(S/100)
  
  temp_2 = 2*L-temp_1
  
  H = H/360
  
  temp_R = H+1/3
  temp_G = H
  temp_B = H-1/3
  
  if temp_R < 0:
    temp_R += 1
  elif temp_R > 1:
    temp_R -= 1
  if temp_G < 0:
    temp_G += 1
  elif temp_G > 1:
    temp_G -= 1
  if temp_B < 0:
    temp_B += 1
  elif temp_B > 1:
    temp_B -= 1
  
  if 6*temp_R < 1:
    R = temp_2+(temp_1-temp_2)*6*temp_R
  elif 2*temp_R < 1:
    R = temp_1
  elif 3*temp_R < 2:
    R = temp_2+(temp_1-temp_2)*(2/3-temp_R)*6
  else:
    R = temp_2
  
  if 6*temp_G < 1:
    G = temp_2+(temp_1-temp_2)*6*temp_G
  elif 2*temp_G < 1:
    G = temp_1
  elif 3*temp_G < 2:
    G = temp_2+(temp_1-temp_2)*(2/3-temp_G)*6
  else:
    G = temp_2
  
  if 6*temp_B < 1:
    B = temp_2+(temp_1-temp_2)*6*temp_B
  elif 2*temp_B < 1:
    B = temp_1
  elif 3*temp_B < 2:
    B = temp_2+(temp_1-temp_2)*(2/3-temp_B)*6
  else:
    B = temp_2
  
  R = round(R*255)
  G = round(G*255)
  B = round(B*255)
  
  return R,G,B

def RGBAtoRGB(x,y,color):
  cpix = get_pixel(x,y)
  if color[3] > 1:a = 1
  elif color[3] < 0:a = 0
  else:a = color[3]
  r = round(color[0]*a+cpix[0]*(1-a))
  g = round(color[1]*a+cpix[1]*(1-a))
  b = round(color[2]*a+cpix[2]*(1-a))
  return (r,g,b)

def buff(x,y,color,i):
  set_pixel(x,y,color)
  if i > 1:
    set_pixel(x+1,y,color)
    buff(x+1,y,color,i-1)
    set_pixel(x,y+1,color)
    buff(x,y+1,color,i-1)
    set_pixel(x-1,y,color)
    buff(x-1,y,color,i-1)
    set_pixel(x,y-1,color)
    buff(x,y-1,color,i-1)

def liney(x1,y1,x2,y2,color,w):
  dx = x2-x1
  dy = y2-y1
  yi = 1
  if dy < 0:
    yi = -1
    dy = -dy
  d = (2*dy)-dx
  y = y1
  for x in range(x1,x2):
    buff(x,y,color,w)
    if d > 0:
      y += yi
      d += 2*(dy-dx)
    else:
      d += 2*dy

def linex(x1,y1,x2,y2,color,w):
  dx = x2-x1
  dy = y2-y1
  xi = 1
  if dx < 0:
    xi = -1
    dx = -dx
  d = (2*dx)-dy
  x = x1
  for y in range(y1,y2):
    buff(x,y,color,w)
    if d > 0:
      x += xi
      d += 2*(dx-dy)
    else:
      d += 2*dx

def line(x1,y1,x2,y2,color,w = 1):
  if abs(y2-y1) < abs(x2-x1):
    if x1 > x2:
      liney(x2,y2,x1,y1,color,w)
    else:
      liney(x1,y1,x2,y2,color,w)
  else:
    if y1 > y2:
      linex(x2,y2,x1,y1,color,w)
    else:
      linex(x1,y1,x2,y2,color,w)

def rect(x,y,w,h,color):
  for i in range(w+1):
    set_pixel(x+i,y,color)
    set_pixel(x+i,y+h,color)
  for i in range(1,h):
    set_pixel(x,y+i,color)
    set_pixel(x+w,y+i,color)

def full_rect(x,y,w,h,color):
  for i in range(w+1):
    for j in range(h+1):
      set_pixel(x+i,y+j,color)

def circle(x,y,r,color):
  xp = 0
  yp = r
  d = 3-2*r
  while yp >= xp:
    if d > 0:
      yp -= 1
      d = d+4*(xp-yp)+10
    else:
      d = d+4*xp+6
    set_pixel(x+xp,y+yp,color)
    set_pixel(x-xp,y+yp,color)
    set_pixel(x+xp,y-yp,color)
    set_pixel(x-xp,y-yp,color)
    set_pixel(x+yp,y+xp,color)
    set_pixel(x-yp,y+xp,color)
    set_pixel(x+yp,y-xp,color)
    set_pixel(x-yp,y-xp,color)
    xp += 1

def full_circle(x,y,r,color):
  for yi in range(-r,r):
    for xi in range(-r,r):
      if xi**2+yi**2 < r**2:
        set_pixel(x+xi,y+yi,color)

def ellipse(x,y,rx,ry,color):
  if rx == ry:
    circle(x,y,rx,color)
    return
  
  for a in range(0,2*360,1):
    xi = round(cos(((a/2)/180)*pi)*rx)
    yi = round(sin(((a/2)/180)*pi)*ry)
    set_pixel(x+xi,y+yi,color)

#from random import randint

clear_screen()
#set_pixel(383,191,(0,0,0))
#set_pixel(191,95,(0,0,0))
#ellipse(191,95,191,95,(0,0,0))
'''for y in range(20):
  for x in range(20):
    set_pixel(x,y,(0,255,0))
    set_pixel(x+40,y,(0,255,0))
for y in range(20):
  for x in range(20):
    set_pixel(x+20,y,(255,0,0))
for y in range(20):
  for x in range(20):
    set_pixel(x+40,y,RGBAtoRGB(x+40,y,(255,0,0,50)))'''
#show_screen()
