11import pygame
22import random
3+ import math
34
45
56def GenerateRandomColorValue ():
6- r = random .randint (0 , 255 )
7- g = random .randint (0 , 255 )
8- b = random .randint (0 , 255 )
7+ # r = random.randint(0, 255)
8+ # g = random.randint(0, 255)
9+ # b = random.randint(0, 255)
10+ r = 255
11+ g = 255
12+ b = 255
13+
914 return (r , g , b )
1015
1116
17+ def ReturnTextColor (colorValue ):
18+ r = colorValue [0 ]
19+ g = colorValue [1 ]
20+ b = colorValue [2 ]
21+ colors = [r , g , b ]
22+
23+ i = 0
24+ for c in colors :
25+ c = c / 255.0
26+ if c <= 0.04045 :
27+ c = c / 12.92
28+ else :
29+ c = math .pow (((c + 0.055 ) / 1.055 ), 2.4 )
30+ colors [i ] = c
31+ i += 1
32+
33+ r = colors [0 ]
34+ g = colors [1 ]
35+ b = colors [2 ]
36+
37+ L = 0.2126 * r + 0.7152 * g + 0.0722 * b
38+
39+ shouldBeWhite = L > 0.179
40+
41+ # shouldBeWhite = (r * 0.299 + g * 0.587 + b * 0.114) > 186
42+ if shouldBeWhite :
43+ return (0 , 0 , 0 )
44+ else :
45+ return (255 , 255 , 255 )
46+
47+
1248pygame .init ()
1349
1450height = 750
@@ -22,7 +58,7 @@ def GenerateRandomColorValue():
2258font = pygame .font .Font (pygame .font .get_default_font (), 32 )
2359
2460# RGB Value
25- RGBText = font .render ("RGB Value: (255 ,255 , 255)" , True , (0 , 0 , 0 ))
61+ RGBText = font .render ("RGB Value: (255, 255, 255)" , True , (0 , 0 , 0 ))
2662RGBTextRect = RGBText .get_rect ()
2763RGBTextRect .center = (width // 2 , height // 2 - 20 )
2864
@@ -44,8 +80,9 @@ def GenerateRandomColorValue():
4480 color = GenerateRandomColorValue ()
4581 RGBString = "RGB Value: " + str (color )
4682 hexString = "Hex Value: " + str ("#%02x%02x%02x" % color )
47- RGBText = font .render (RGBString , True , (0 , 0 , 0 ))
48- hexText = font .render (hexString , True , (0 , 0 , 0 ))
83+ TextColor = ReturnTextColor (color )
84+ RGBText = font .render (RGBString , True , TextColor )
85+ hexText = font .render (hexString , True , TextColor )
4986 print (RGBString + "; " + hexString )
5087
5188 canvas .fill (color )
0 commit comments