Color Selection in Python

Choosing a color could be done using buttons, as it done in the Paint pro­gram, but that limits the number of colors significantly. How many buttons can be provided? Instead, Mondrean provides a color wheel that shows a large number of distinct colors. When the user clicks on a pixel in this wheel, that color is used as the current color. Simple for the user.

Figure 15.5 shows the color wheel, which is drawn at (500,680) in the display. That’s the upper left coordinate of the image, which is 200 x 200 pixels in size. This means that the center of the circle is at 500+100, 680+100, or 600, 780. The radius is 100. Thus, if the distance from the mouse position to (600,780) is less than 100 when the mouse button is clicked, then the user is selecting a color. What color? The one right beneath the mouse cursor, of course. This is acquired using the call

currentColor = screen.get at((mouseX, mouseY))

where the variables mouseX and mouseY hold the current mouse position as returned by the call

pygame.mouse.get pos()

in the event loop. The variable currentColor always contains the color being used to draw with.

After this was implemented, it was discovered that there was no way to select any shade of grey, including the very popular colors white and black. A second selector was added to the color disk, in this case, a rectangular one that has a color range from black through grey values to white. When the mouse button is released in this region, it sets the color to the grey level that is under the mouse cursor.

Source: Parker James R. (2021), Python: An Introduction to Programming, Mercury Learning and Information; Second edition.

Leave a Reply

Your email address will not be published. Required fields are marked *