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 12, 2020
1 parent fc08e98 commit a8e5335
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 30 deletions.
12 changes: 12 additions & 0 deletions img2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from PIL import Image

img = Image.open('newDa/hue4.png')
img = img.convert('RGBA')
pixel = img.load()
w, h = img.size
for x in range(w):
for y in range(h):
if pixel[x, y] != (0, 0, 0, 0):
pixel[x, y] = (0, 0, 0, 255)

img.save('newDa/value_wheel.png')
8 changes: 5 additions & 3 deletions makeBar.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from PIL import Image

img = Image.new('RGBA', (200, 20), (0, 0, 0, 0))
img = Image.open('newDa/alpha2.png')
img = img.convert('RGBA')
pixel = img.load()
w, h = img.size
for x in range(w):
for y in range(h):
pixel[x, y] = (0, 0, 0, round((w - x - 1) / (w - 1) * 255))
img.save('newDa/value.png')
_r, _g, _b, _ = pixel[x, y]
pixel[x, y] = (_r, _g, _b, round((w - x - 1) / (w - 1) * 255))
img.save('newDa/alpha_bar.png')
Binary file added newDa/alpha2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added newDa/cdrc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added newDa/value_wheel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
111 changes: 90 additions & 21 deletions src/Section/ColorSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,83 +7,152 @@

R, G, B = 0, 1, 2
H, S, V = 0, 1, 2
WHEEL, VALUE, ALPHA = 0, 1, 2

class ColorSection(Section):
colorCenterX: int
colorCenterY: int

bgColor = (43, 43, 43)

colorRGB = (166, 106, 150)
colorHSV = utility.RGB2HSV(colorRGB)
alpha = 255

colorWheelImage = pg.image.load('data/hue4.png')
valueWheelImage = pg.image.load('data/value_wheel.png')
# radius = colorWheelImage.get_width() // 2
radius = 100
upperTerm = 15
colorRGB = (0, 255, 0)
colorHSV = utility.RGB2HSV(colorRGB)
print(colorRGB, colorHSV)
dotImage = pg.image.load('data/dot.png')
dotRadius = dotImage.get_width() // 2
radiusTerm = colorWheelImage.get_width() // 2 - radius
print(radius, radiusTerm, colorWheelImage.get_width())

colorChange = False
dotImage: pg.Surface
dotDarkImage = pg.image.load('data/dot_dark.png')
dotBrightImage = pg.image.load('data/dot_bright.png')
dotRadius = 3

barWidth = 200
barHeight = 20
valueImage = pg.image.load('data/value.png')
alphaImage = pg.image.load('data/alpha.png')
valueBarRect: pg.Rect
alphaBarRect: pg.Rect

colorChange = [0, 0, 0]

# ----- for test -----
wheelCenterX = 120
wheelCenterY = 120

def Setup(self, x, y, w, h):
super().Setup(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.barWidth,
self.barHeight)
self.alphaBarRect = pg.Rect((self.w - self.barWidth) // 2,
self.wheelCenterY + self.radius + self.radiusTerm + 15 + 1 + self.barHeight + 5,
self.barWidth,
self.barHeight)

def Update(self):
# print(self.w - self.barWidth)
# print(self.wheelCenterY + self.radius + self.radiusTerm + 15 + 1)
self.surface.fill(self.bgColor)
self.DrawColor()
self.DrawColorWheel()
self.DrawColorBar()

def SetColorRGB(self, color: (int, int, int)):
if len(color) == 4:
_r, _g, _b, _ = color
else:
_r, _g, _b = color
self.colorRGB = (_r, _g, _b)
self.colorHSV = utility.RGB2HSV(self.colorRGB)
self.Changed()
Brush.pencil.SetCurrentColor(self.colorRGB)
self.SetColorHSV(utility.RGB2HSV(self.colorRGB))

def SetColorHSV(self, color: (float, float, float)):
self.colorHSV = color
self.colorRGB = utility.HSV2RGB(color)
self.Changed()
Brush.pencil.SetCurrentColor(self.colorRGB)
Brush.pencil.SetCurrentColor((*self.colorRGB, self.alpha))
if self.colorHSV[V] > 50:
self.dotImage = self.dotDarkImage
else:
self.dotImage = self.dotBrightImage

def DrawColor(self):
def SetAlpha(self, alpha):
self.alpha = alpha
Brush.pencil.SetCurrentColor((*self.colorRGB, self.alpha))
self.Changed()

def DrawColorWheel(self):
_theta = radians(90 - self.colorHSV[H])
_x = round(cos(_theta) * self.radius * self.colorHSV[S] / 100)
_y = -round(sin(_theta) * self.radius * self.colorHSV[S] / 100)
# print(_x, _y)
self.surface.blit(self.colorWheelImage,
(self.wheelCenterX - self.radius - self.radiusTerm,
self.wheelCenterY - self.radius - self.radiusTerm))
self.valueWheelImage.set_alpha(255 - round(self.colorHSV[V] / 100 * 255))
self.surface.blit(self.valueWheelImage,
(self.wheelCenterX - self.radius - self.radiusTerm,
self.wheelCenterY - self.radius - self.radiusTerm))
self.surface.blit(self.dotImage,
(self.wheelCenterX + _x - self.dotRadius,
self.wheelCenterY + _y - self.dotRadius))

def DrawColorBar(self):
_baseColor = (self.colorHSV[H], self.colorHSV[S], 100)

pg.draw.rect(self.surface, utility.HSV2RGB(_baseColor), self.valueBarRect)
pg.draw.rect(self.surface, self.colorRGB, self.alphaBarRect)
self.surface.blit(self.valueImage, self.valueBarRect.topleft)
self.surface.blit(self.alphaImage, self.alphaBarRect.topleft)
self.surface.blit(self.dotImage, (self.valueBarRect.x + self.colorHSV[V] / 100 * self.barWidth - self.dotRadius,
self.valueBarRect.centery - self.dotRadius))
self.surface.blit(self.dotImage, (self.alphaBarRect.x + self.alpha / 255 * self.barWidth - self.dotRadius,
self.alphaBarRect.centery - self.dotRadius))

def Position2HSV(self, x, y) -> (float, float, float):
_theta = atan2(self.wheelCenterY - y, x - self.wheelCenterX)
_h = 90 - degrees(_theta)
if _h < 0:
_h += 360
_s = min(self.DistToOrigin(x, y), 100)
_v = self.colorHSV[V]
return _h, _s, _v

def Position2Value(self, x) -> (float, float, float):
_dx = max(min(x - self.valueBarRect.x, self.barWidth), 0)
_v = _dx / self.barWidth * 100
_h, _s, _ = self.colorHSV
return _h, _s, _v

def Position2Alpha(self, x) -> int:
_dx = max(min(x - self.valueBarRect.x, self.barWidth), 0)
return round(_dx / self.barWidth * 255)

def DistToOrigin(self, x, y) -> float:
return sqrt((x - self.wheelCenterX) ** 2 + (y - self.wheelCenterY) ** 2)

def OnMouseDown(self, button, x, y):
x, y = self.LocalPosition((x, y))
if self.DistToOrigin(x, y) < self.radius + self.radiusTerm:
self.colorChange = True
if button == 1:
if self.DistToOrigin(x, y) < self.radius + self.radiusTerm:
self.colorChange[WHEEL] = 1
elif self.valueBarRect.collidepoint(x, y):
self.colorChange[VALUE] = 1
elif self.alphaBarRect.collidepoint(x, y):
self.colorChange[ALPHA] = 1

def OnMouseDrag(self, button, x, y, _x, _y):
x, y = self.LocalPosition((x, y))
if self.colorChange:
if self.colorChange[WHEEL]:
self.SetColorHSV(self.Position2HSV(x, y))
elif self.colorChange[VALUE]:
self.SetColorHSV(self.Position2Value(x))
elif self.colorChange[ALPHA]:
self.SetAlpha(self.Position2Alpha(x))

def OnMouseUp(self, button, x, y):
self.colorChange = False


self.colorChange = [0, 0, 0]
4 changes: 0 additions & 4 deletions src/Section/_Section.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class Section:
y: int
w: int
h: int
centerX: int
centerY: int
bgColor: (int, int, int)
rect: pg.Rect
surface: pg.Surface
Expand All @@ -21,8 +19,6 @@ def Setup(self, x, y, w, h):
self.y = y + self.term
self.w = w - self.term * 2
self.h = h - self.term * 2
self.centerX = self.w // 2
self.centerY = self.h // 2
self.rect = pg.Rect(self.x, self.y, self.w, self.h)
self.surface = pg.Surface((self.w, self.h), pg.SRCALPHA, 32)
self._hasChange = True
Expand Down
3 changes: 3 additions & 0 deletions src/TestSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

mouse = [0, 0, 0, 0]

clock = pg.time.Clock()

while 1:
preX, preY = mouseX, mouseY
mouseX, mouseY = pg.mouse.get_pos()
Expand All @@ -39,3 +41,4 @@


section.Draw(screen)
clock.tick(126)
3 changes: 1 addition & 2 deletions src/Window.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class MainWindow:
_diff = 200

Brush.SetBrush('Pencil')
# ----- for test -----
Brush.pencil.SetCurrentColor((255, 0, 127, 255))

CanvasSection.Setup(originX, 0, w - originX, originY)
CanvasSection.SetupCanvas(64, 64)
Expand Down Expand Up @@ -112,6 +110,7 @@ def EventFeedback(self, event):
elif event.type == MOUSEBUTTONUP:
if event.button < len(self.mouseButton):
self.mouseButton[event.button] = 0
self.currentSection.OnMouseUp(event.button, self.mouseX, self.mouseY)

def LateFeedback(self):
for _button in range(1, self.mouseButtonCount):
Expand Down
Binary file added src/data/alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/data/dot_bright.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added src/data/value.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/data/value_wheel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def RGB2HSV(color: (int, int, int)) -> (float, float, float):

def HSV2RGB(color: (float, float, float)) -> (int, int, int):
_h, _s, _v = color
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)

Expand Down

0 comments on commit a8e5335

Please sign in to comment.