Skip to content

Commit

Permalink
Merge pull request #182 from 101zh/master
Browse files Browse the repository at this point in the history
Random Color Generator Script
  • Loading branch information
DhanushNehru authored Oct 15, 2023
2 parents 0191196 + 95342b9 commit 087fa57
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ More information on contributing and the general code of conduct for discussion
| Planet Simulation | [Planet Simulation](https://github.com/DhanushNehru/Python-Scripts/tree/master/planetSimulation) | A simulation of several planets rotating around the sun. |
| Remove Background | [Remove Background](https://github.com/DhanushNehru/Python-Scripts/tree/master/Remove%20Background) | Removes the background of images. |
| ROCK-PAPER-SCISSOR | [ROCK-PAPER-SCISSOR](https://github.com/DhanushNehru/Python-Scripts/tree/master/ROCK-PAPER-SCISSOR) | A game of Rock Paper Scissors. |
| Random Color Generator | [Random Color Generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Random%20Color%20Generator) | A random color generator that will show you the color and values!
| Run Then Notify | [Run Then Notify](https://github.com/DhanushNehru/Python-Scripts/tree/master/Run%20Then%20Notify) | Runs a slow command and mails you when it completes execution. |
| Selfie with Python | [Selfie_with_Python](https://github.com/DhanushNehru/Python-Scripts/tree/master/Selfie_with_Python) | Take your selfie with python . |
| Simple TCP Chat Server | [Simple TCP Chat Server](https://github.com/DhanushNehru/Python-Scripts/tree/master/TCP%20Chat%20Server) | Creates a local server on your LAN for receiving and sending messages! |
Expand Down
25 changes: 25 additions & 0 deletions Random Color Generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# How to Run
- Make sure you have python 3.8.10 installed
- Make sure you have pygame installed \(How to install [Pygame](#how-to-install-pygame)\)
- Extract Random Color Generator.zip
- Run the program with
```markdown
./Random Color Generator> python RandColorGen.py
```

# How to use
- Click on the window to change colors
- The color values are logged in the console and shown on screen

# How to Install Pygame
- After installing python
- Use pip to install Pygame
```markdown
./wherever> pip install pygame
```

## Dependencies:
- random library
- math library
- pygame library

87 changes: 87 additions & 0 deletions Random Color Generator/RandColorGen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import pygame
import random
import math


def GenerateRandomColorValue():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)

return (r, g, b)


def ReturnTextColor(colorValue):
r = colorValue[0]
g = colorValue[1]
b = colorValue[2]
colors = [r, g, b]

i = 0
for c in colors:
c = c / 255.0
if c <= 0.04045:
c = c / 12.92
else:
c = math.pow(((c + 0.055) / 1.055), 2.4)
colors[i] = c
i += 1

r = colors[0]
g = colors[1]
b = colors[2]

L = 0.2126 * r + 0.7152 * g + 0.0722 * b

shouldBeWhite = L > 0.179
# shouldBeWhite = (r * 0.299 + g * 0.587 + b * 0.114) > 186

if shouldBeWhite:
return (0, 0, 0)
else:
return (255, 255, 255)


pygame.init()

height = 500
width = 500

canvas = pygame.display.set_mode((width, height))
pygame.display.set_caption("Random Color Generator!")
isExit = False
canvas.fill((255, 255, 255))

font = pygame.font.Font(pygame.font.get_default_font(), 32)

# RGB Value
RGBText = font.render("RGB Value: (255, 255, 255)", True, (0, 0, 0))
RGBTextRect = RGBText.get_rect()
RGBTextRect.center = (width // 2, height // 2 - 20)

# Hex Value
hexText = font.render("Hex Value: #ffffff", True, (0, 0, 0))
hexTextRect = hexText.get_rect()
hexTextRect.center = (width // 2, height // 2 + 20)
hexTextRect.left = RGBTextRect.left


while not isExit:
canvas.blit(RGBText, RGBTextRect)
canvas.blit(hexText, hexTextRect)

for event in pygame.event.get():
if event.type == pygame.QUIT:
isExit = True
if event.type == pygame.MOUSEBUTTONUP:
color = GenerateRandomColorValue()
RGBString = "RGB Value: " + str(color)
hexString = "Hex Value: " + str("#%02x%02x%02x" % color)
TextColor = ReturnTextColor(color)
RGBText = font.render(RGBString, True, TextColor)
hexText = font.render(hexString, True, TextColor)
print(RGBString + "; " + hexString)

canvas.fill(color)

pygame.display.flip()
Binary file added Random Color Generator/Random Color Generator.zip
Binary file not shown.

0 comments on commit 087fa57

Please sign in to comment.