Advertisement
AdmiralAckbar31

Rectangular Color Picker

Dec 10th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. #Program to pick color from a
  2. #rectangular display
  3.  
  4. from graphics import *
  5. #HSV to RGB color conversion from Wikipedia article
  6. def colors(hue, sat, val):
  7.  
  8.     h = hue
  9.     s = sat / 255
  10.     v = val
  11.  
  12.     c = v * s
  13.     m = v - c
  14.     h = h / 60
  15.     x = c * (1 - abs((h % 2) - 1))
  16.  
  17.     if h >= 0 and h <= 1:
  18.         r = c
  19.         g = x
  20.         b = 0
  21.     elif h >= 1 and h <= 2:
  22.         r = x
  23.         g = c
  24.         b = 0
  25.     elif h >= 2 and h <= 3:
  26.         r = 0
  27.         g = c
  28.         b = x
  29.     elif h >= 3 and h <= 4:
  30.         r = 0
  31.         g = x
  32.         b = c
  33.     elif h >= 4 and h <= 5:
  34.         r = x
  35.         g = 0
  36.         b = c
  37.     elif h >= 5 and h <= 6:
  38.         r = c
  39.         g = 0
  40.         b = x
  41.  
  42.     r = 255 * (r + m)
  43.     g = 255 * (g + m)
  44.     b = 255 * (b + m)
  45.  
  46.     color = color_rgb(int(r), int(g), int(b))
  47.  
  48.     return color
  49.  
  50. def window():
  51.  
  52.     win = GraphWin("Color Selection", 1000, 500, autoflush=False)
  53.     win.setCoords(-100, -100, 600, 400)
  54.     win.setBackground("Gray")
  55.     #make the rectangle to choose the color out of
  56.     for y in range(255, 0, -1):#for saturation, 0-255 for R,G,B
  57.  
  58.         for x in range(360):#for hue, 360 for R,G,B
  59.  
  60.             color = colors(x, y, 1)
  61.  
  62.             cube = Rectangle(Point(x, y), Point(x, y))
  63.             cube.setFill(color)
  64.             cube.setOutline(color)
  65.             cube.draw(win)
  66.     #title
  67.     txt = Text(Point(180, 300), "Color Picker")
  68.     txt.draw(win)
  69.     #designates area that will be filled with chosen color
  70.     cube = Rectangle(Point(400, 0), Point(450, 50))
  71.     cube.setOutline("light gray")
  72.     cube.setFill("light gray")
  73.     cube.draw(win)
  74.     #designates area that will be filled with gradient of chosen color
  75.     cube = Rectangle(Point(400, 60), Point(450, 255))
  76.     cube.setOutline("light gray")
  77.     cube.setFill("light gray")
  78.     cube.draw(win)
  79.  
  80.     return win
  81.  
  82. def main():
  83.  
  84.     new_color = {}
  85.  
  86.     win = window()
  87.  
  88.     update()
  89.  
  90.     z = 1
  91.     #hold program open allowing the user to make mouse clicks as much as they want
  92.     while z == 1:
  93.  
  94.         click = win.getMouse()
  95.  
  96.         x = click.getX()
  97.  
  98.         y = click.getY()
  99.  
  100.         if 450 >= x >= 400 and 255 >= y >= 60 and len(new_color) > 0:
  101.  
  102.             cube = Rectangle(Point(400, 0), Point(450, 50))
  103.             color = new_color[str(int((y-60)/2))]
  104.             cube.setOutline(color)
  105.             cube.setWidth(1)
  106.             cube.setFill(color)
  107.             cube.draw(win)
  108.  
  109.         else:
  110.  
  111.             if 0 > x or x > 360 or 0 > y or y > 255:
  112.                 x = 0
  113.                 y = 0
  114.  
  115.             new_color.clear()
  116.             cube = Rectangle(Point(400, 0), Point(450, 50))
  117.             cube.draw(win)
  118.  
  119.             for v in range(100, 0, -1):
  120.  
  121.                 color = colors(x, y, v/100)
  122.  
  123.                 if v == 100:
  124.                     #draw the initial chosen color in the designated area on the window
  125.                     cube = Rectangle(Point(400, 0), Point(450, 50))
  126.                     cube.setOutline(color)
  127.                     cube.setWidth(1)
  128.                     cube.setFill(color)
  129.                     cube.draw(win)
  130.                 #draw the gradient in the designated area on the window
  131.                 new_color[str(v)] = color
  132.                 cube = Rectangle(Point(400, 60), Point(450, 255))
  133.                 cube.setOutline(color)
  134.                 cube.setWidth(1)
  135.                 cube.setFill(color)
  136.                 cube.draw(win)
  137.  
  138. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement