Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
Signed-off-by: Thxios <[email protected]>
  • Loading branch information
Thxios committed Mar 23, 2020
1 parent c607ada commit 4fa6220
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 7 deletions.
Binary file added newDa/PaletteSection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions src/Color.py
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)


8 changes: 4 additions & 4 deletions src/Section/ColorSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ def Setup(self, x, y, w, h):

self.SetColorRGB(self.colorRGB)
self.valueBarRect = pg.Rect((self.w - self.barWidth) // 2,
self.wheelCenterY + self.radius + self.radiusTerm + 15 + 1,
self.wheelCenterY + self.radius + self.radiusTerm + 13 + 1,
self.barWidth,
self.barHeight)
self.alphaBarRect = pg.Rect((self.w - self.barWidth) // 2,
self.wheelCenterY + self.radius + self.radiusTerm + 15 + 1 + self.barHeight + 5,
self.wheelCenterY + self.radius + self.radiusTerm + 13 + 1 + self.barHeight + 5,
self.barWidth,
self.barHeight)

Expand Down Expand Up @@ -120,8 +120,8 @@ def DrawColorBar(self):

def DrawPreview(self):
self.preViewSurface.fill((*self.colorRGB, self.alpha))
self.surface.blit(self.previewAlphaImage, (20, 310))
self.surface.blit(self.preViewSurface, (20, 310))
self.surface.blit(self.previewAlphaImage, (20, 300))
self.surface.blit(self.preViewSurface, (20, 300))

def Position2HSV(self, x, y) -> (float, float, float):
_theta = atan2(self.wheelCenterY - y, x - self.wheelCenterX)
Expand Down
75 changes: 74 additions & 1 deletion src/Section/PaletteSection.py
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)
4 changes: 2 additions & 2 deletions src/TestSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from src.Section.Section import *


section = BrushSection
section = PaletteSection
pg.init()

w, h = 66, 250
w, h = 198, 250
section.Setup(0, 0, w, h)

screen = pg.display.set_mode((w, h))
Expand Down
8 changes: 8 additions & 0 deletions src/palette/0.txt
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

0 comments on commit 4fa6220

Please sign in to comment.