Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Program to pick color from a
- #rectangular display
- from graphics import *
- #HSV to RGB color conversion from Wikipedia article
- def colors(hue, sat, val):
- h = hue
- s = sat / 255
- v = val
- c = v * s
- m = v - c
- h = h / 60
- x = c * (1 - abs((h % 2) - 1))
- if h >= 0 and h <= 1:
- r = c
- g = x
- b = 0
- elif h >= 1 and h <= 2:
- r = x
- g = c
- b = 0
- elif h >= 2 and h <= 3:
- r = 0
- g = c
- b = x
- elif h >= 3 and h <= 4:
- r = 0
- g = x
- b = c
- elif h >= 4 and h <= 5:
- r = x
- g = 0
- b = c
- elif h >= 5 and h <= 6:
- r = c
- g = 0
- b = x
- r = 255 * (r + m)
- g = 255 * (g + m)
- b = 255 * (b + m)
- color = color_rgb(int(r), int(g), int(b))
- return color
- def window():
- win = GraphWin("Color Selection", 1000, 500, autoflush=False)
- win.setCoords(-100, -100, 600, 400)
- win.setBackground("Gray")
- #make the rectangle to choose the color out of
- for y in range(255, 0, -1):#for saturation, 0-255 for R,G,B
- for x in range(360):#for hue, 360 for R,G,B
- color = colors(x, y, 1)
- cube = Rectangle(Point(x, y), Point(x, y))
- cube.setFill(color)
- cube.setOutline(color)
- cube.draw(win)
- #title
- txt = Text(Point(180, 300), "Color Picker")
- txt.draw(win)
- #designates area that will be filled with chosen color
- cube = Rectangle(Point(400, 0), Point(450, 50))
- cube.setOutline("light gray")
- cube.setFill("light gray")
- cube.draw(win)
- #designates area that will be filled with gradient of chosen color
- cube = Rectangle(Point(400, 60), Point(450, 255))
- cube.setOutline("light gray")
- cube.setFill("light gray")
- cube.draw(win)
- return win
- def main():
- new_color = {}
- win = window()
- update()
- z = 1
- #hold program open allowing the user to make mouse clicks as much as they want
- while z == 1:
- click = win.getMouse()
- x = click.getX()
- y = click.getY()
- if 450 >= x >= 400 and 255 >= y >= 60 and len(new_color) > 0:
- cube = Rectangle(Point(400, 0), Point(450, 50))
- color = new_color[str(int((y-60)/2))]
- cube.setOutline(color)
- cube.setWidth(1)
- cube.setFill(color)
- cube.draw(win)
- else:
- if 0 > x or x > 360 or 0 > y or y > 255:
- x = 0
- y = 0
- new_color.clear()
- cube = Rectangle(Point(400, 0), Point(450, 50))
- cube.draw(win)
- for v in range(100, 0, -1):
- color = colors(x, y, v/100)
- if v == 100:
- #draw the initial chosen color in the designated area on the window
- cube = Rectangle(Point(400, 0), Point(450, 50))
- cube.setOutline(color)
- cube.setWidth(1)
- cube.setFill(color)
- cube.draw(win)
- #draw the gradient in the designated area on the window
- new_color[str(v)] = color
- cube = Rectangle(Point(400, 60), Point(450, 255))
- cube.setOutline(color)
- cube.setWidth(1)
- cube.setFill(color)
- cube.draw(win)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement