diff --git a/img2.py b/img2.py new file mode 100644 index 0000000..147e827 --- /dev/null +++ b/img2.py @@ -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') diff --git a/makeBar.py b/makeBar.py index 6f35fbc..8ee7424 100644 --- a/makeBar.py +++ b/makeBar.py @@ -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') diff --git a/newDa/alpha2.png b/newDa/alpha2.png new file mode 100644 index 0000000..0bf235b Binary files /dev/null and b/newDa/alpha2.png differ diff --git a/newDa/cdrc.png b/newDa/cdrc.png new file mode 100644 index 0000000..18ae615 Binary files /dev/null and b/newDa/cdrc.png differ diff --git a/newDa/value_wheel.png b/newDa/value_wheel.png new file mode 100644 index 0000000..0247f49 Binary files /dev/null and b/newDa/value_wheel.png differ diff --git a/src/Section/ColorSection.py b/src/Section/ColorSection.py index 07cf6c9..6c0c61d 100644 --- a/src/Section/ColorSection.py +++ b/src/Section/ColorSection.py @@ -7,51 +7,85 @@ 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) @@ -59,31 +93,66 @@ def DrawColor(self): 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] diff --git a/src/Section/_Section.py b/src/Section/_Section.py index f6fd9e3..f2e7752 100644 --- a/src/Section/_Section.py +++ b/src/Section/_Section.py @@ -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 @@ -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 diff --git a/src/TestSection.py b/src/TestSection.py index b73411c..a00cdbc 100644 --- a/src/TestSection.py +++ b/src/TestSection.py @@ -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() @@ -39,3 +41,4 @@ section.Draw(screen) + clock.tick(126) diff --git a/src/Window.py b/src/Window.py index 409a32a..32ae692 100644 --- a/src/Window.py +++ b/src/Window.py @@ -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) @@ -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): diff --git a/src/data/alpha.png b/src/data/alpha.png new file mode 100644 index 0000000..4fbde8d Binary files /dev/null and b/src/data/alpha.png differ diff --git a/src/data/dot_bright.png b/src/data/dot_bright.png new file mode 100644 index 0000000..79c825f Binary files /dev/null and b/src/data/dot_bright.png differ diff --git a/src/data/dot.png b/src/data/dot_dark.png similarity index 100% rename from src/data/dot.png rename to src/data/dot_dark.png diff --git a/src/data/value.png b/src/data/value.png new file mode 100644 index 0000000..6a81494 Binary files /dev/null and b/src/data/value.png differ diff --git a/src/data/value_wheel.png b/src/data/value_wheel.png new file mode 100644 index 0000000..0247f49 Binary files /dev/null and b/src/data/value_wheel.png differ diff --git a/src/utility.py b/src/utility.py index 7812c5e..e701b75 100644 --- a/src/utility.py +++ b/src/utility.py @@ -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)