-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Thxios <[email protected]>
- Loading branch information
Showing
6 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from src.lib import * | ||
from src import utility | ||
import colorsys | ||
|
||
|
||
class Color: | ||
def __init__(self, r, g, b, a=255): | ||
self._r = r | ||
self._g = g | ||
self._b = b | ||
self._a = a | ||
|
||
self._h, self._s, self._v = Color.RGB2HSV((r, g, b)) | ||
|
||
@property | ||
def rgb(self) -> (int, int, int): | ||
return self._r, self._g, self._b | ||
|
||
@rgb.setter | ||
def rgb(self, rgb: (int, int, int)): | ||
self._r, self._g, self._b = rgb | ||
self._h, self._s, self._v = Color.RGB2HSV(self.rgb) | ||
|
||
@property | ||
def rgba(self) -> (int, int, int, int): | ||
return self._r, self._g, self._b, self._a | ||
|
||
@rgba.setter | ||
def rgba(self, rgba: (int, int, int, int)): | ||
self._r, self._g, self._b, self._a = rgba | ||
self._h, self._s, self._v = Color.RGB2HSV(self.rgb) | ||
|
||
@property | ||
def hsv(self) -> (float, float, float): | ||
return self._h, self._s, self._v | ||
|
||
@hsv.setter | ||
def hsv(self, hsv: (float, float, float)): | ||
self._h, self._s, self._v = hsv | ||
self._r, self._g, self._b = Color.HSV2RGB(self.hsv) | ||
|
||
@staticmethod | ||
def RGB2HSV(rgb: (int, int, int)) -> (float, float, float): | ||
r, g, b = rgb | ||
_h, _s, _v = colorsys.rgb_to_hsv(r / 255, g / 255, b / 255) | ||
return _h * 360, _s * 100, _v * 100 | ||
|
||
@staticmethod | ||
def HSV2RGB(hsv: (float, float, float)) -> (int, int, int): | ||
_h, _s, _v = hsv | ||
if _h < 0: | ||
raise TypeError('Hue value is invalid') | ||
_r, _g, _b = colorsys.hsv_to_rgb(_h / 360, _s / 100, _v / 100) | ||
return round(_r * 255), round(_g * 255), round(_b * 255) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,80 @@ | ||
import pygame as pg | ||
from src.lib import * | ||
from src.Section._Section import Section | ||
|
||
|
||
class PaletteSection(Section): | ||
bgColor = (43, 43, 43) | ||
paletteBoxColor = (0, 0, 0) | ||
paletteBoxTerm = 5 | ||
|
||
colorBoxSize = 30 | ||
colorBoxTerm = 3 | ||
|
||
color: List[Tuple[int, int, int, int]] = [] | ||
colorCount = 0 | ||
colorRect: List[pg.Rect] = [] | ||
|
||
# ----- for test ----- | ||
palettePath = 'palette/0.txt' | ||
|
||
def Setup(self, x, y, w, h): | ||
super().Setup(x, y, w, h) | ||
|
||
self.ImportPalette(self.palettePath) | ||
|
||
def Update(self): | ||
self.surface.fill(self.bgColor) | ||
self.MakeColorRect() | ||
print(self.color) | ||
print(self.colorRect) | ||
|
||
for i, colorRect in enumerate(self.colorRect): | ||
pg.draw.rect(self.surface, self.paletteBoxColor, | ||
colorRect.inflate(2 * self.colorBoxTerm, 2 * self.colorBoxTerm)) | ||
pg.draw.rect(self.surface, self.color[i], colorRect) | ||
|
||
def MakeColorRect(self): | ||
self.colorRect = [] | ||
n = (self.rect.w - 2 * self.paletteBoxTerm - self.colorBoxTerm) // (self.colorBoxSize + self.colorBoxTerm) | ||
for i, color in enumerate(self.color): | ||
x, y = i % n, i // n | ||
self.colorRect.append(pg.Rect( | ||
self.paletteBoxTerm + self.colorBoxTerm + x * (self.colorBoxSize + self.colorBoxTerm), | ||
self.paletteBoxTerm + self.colorBoxTerm + y * (self.colorBoxSize + self.colorBoxTerm), | ||
self.colorBoxSize, | ||
self.colorBoxSize | ||
)) | ||
|
||
def OnMouseDown(self, button, x, y): | ||
x, y = self.LocalPosition((x, y)) | ||
if button == 1: | ||
pass | ||
elif button == 4: | ||
pass | ||
elif button == 5: | ||
pass | ||
|
||
def AddColor(self, color: (int, int, int)): | ||
if len(color) == 3: | ||
r, g, b = color | ||
a = 255 | ||
elif len(color) == 4: | ||
r, g, b, a = color | ||
else: | ||
return | ||
|
||
self.color.append((r, g, b, a)) | ||
self.colorCount += 1 | ||
self.Changed() | ||
|
||
def ImportPalette(self, path): | ||
try: | ||
with open(path, 'r') as f: | ||
colors = f.readlines() | ||
except FileNotFoundError: | ||
print('Palette File Not Found in:', path) | ||
return | ||
for color in colors: | ||
rgb = tuple(map(int, color.split())) | ||
if len(rgb) == 3 or len(rgb) == 4: | ||
self.AddColor(rgb) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
255 255 255 | ||
255 0 0 | ||
0 255 0 | ||
0 0 255 | ||
255 255 0 | ||
255 0 255 | ||
0 255 255 | ||
0 0 0 |