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 70bb34e commit fc08e98
Show file tree
Hide file tree
Showing 19 changed files with 253 additions and 148 deletions.
4 changes: 3 additions & 1 deletion .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions .idea/PixelEditor.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions img.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
if pixel[x, y] == (0, 0, 0, 255):
pixel[x, y] = (0, 0, 0, 0)

img.thumbnail((201, 201))
img.save('newDa/hue3.png')
img.thumbnail((201 + 6, 201 + 6))
img.save('newDa/hue4.png')
9 changes: 9 additions & 0 deletions makeBar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from PIL import Image

img = Image.new('RGBA', (200, 20), (0, 0, 0, 0))
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')
Binary file added newDa/ColorSection.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/alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion src/Brush.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ def OnMouseUp(self, clickedPixel, layer: Layer):
pass

def SetCurrentColor(self, color):
self.currentColor = color
if len(color) == 3:
_r, _g, _b = color
_a = 255
else:
_r, _g, _b, _a = color
self.currentColor = (_r, _g, _b, _a)


class _PencilBrush(_Brush):
Expand Down
2 changes: 1 addition & 1 deletion src/HWwindow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pygame as pg
from pygame.locals import *
from src.Section import CanvasSection, ColorSection
from src.Section.Section import CanvasSection, ColorSection
from src.Brush import Brush
from OpenGL.GL import *
from OpenGL.GLU import *
Expand Down
158 changes: 22 additions & 136 deletions src/Section.py → src/Section/CanvasSection.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,8 @@
import pygame as pg
from src.Sprite import Sprite
from src.Section._Section import Section
from src.Brush import Brush
from math import ceil, sin, cos, atan2, radians, degrees
from src import utility

_tileSize = 32
_gray1 = pg.Surface((_tileSize, _tileSize), pg.SRCALPHA, 32)
_gray2 = pg.Surface((_tileSize, _tileSize), pg.SRCALPHA, 32)
_gray1.fill((127, 127, 127, 255))
_gray2.fill((192, 192, 192, 255))


class Section:
x: int
y: int
w: int
h: int
centerX: int
centerY: int
bgColor: (int, int, int)
rect: pg.Rect
surface: pg.Surface
_hasChange: bool

_outlineColor = (255, 255, 255)
term = 5

def Setup(self, x, y, w, h):
self.x = x + self.term
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

def SetBackgroundColor(self, color):
self.bgColor = color

def OnMouseDown(self, button, x, y):
pass

def OnMouseDrag(self, button, x, y, _x, _y):
pass

def OnMouseUp(self, button, x, y):
pass

def Changed(self):
self._hasChange = True

def Draw(self, screen):
if self._hasChange:
self.Update()
screen.blit(self.surface, (self.x, self.y))
pg.display.update(pg.draw.rect(screen, self._outlineColor, self.rect, 3))
self._hasChange = False

def IsClicked(self, mousePos) -> bool:
return self.rect.collidepoint(*mousePos)

def LocalPosition(self, position) -> (int, int):
_x, _y = position
return _x - self.x, _y - self.y

def Update(self):
self.surface.fill(self.bgColor)
from src.Sprite import Sprite
from math import ceil


class CanvasSection(Section):
Expand All @@ -85,6 +20,12 @@ class CanvasSection(Section):
bgColor = (60, 63, 65)
canvasOutlineWidth = 2

tileSize = 32
_gray1 = pg.Surface((tileSize, tileSize), pg.SRCALPHA, 32)
_gray2 = pg.Surface((tileSize, tileSize), pg.SRCALPHA, 32)
_gray1.fill((127, 127, 127, 255))
_gray2.fill((192, 192, 192, 255))

def SetupCanvas(self, w, h):
self.sprite = Sprite.Empty(w, h)
self.canvasWidth = w
Expand All @@ -94,12 +35,12 @@ def SetupCanvas(self, w, h):
self.canvasSurface = self.sprite.GetSurface()

self.backgroundOriginal = pg.Surface((self.canvasWidth, self.canvasHeight), pg.SRCALPHA, 32)
for _x in range(ceil(self.canvasWidth / _tileSize)):
for _y in range(ceil(self.canvasHeight / _tileSize)):
for _x in range(ceil(self.canvasWidth / self.tileSize)):
for _y in range(ceil(self.canvasHeight / self.tileSize)):
if (_x + _y) % 2:
self.backgroundOriginal.blit(_gray1, (_x * _tileSize, _y * _tileSize))
self.backgroundOriginal.blit(self._gray1, (_x * self.tileSize, _y * self.tileSize))
else:
self.backgroundOriginal.blit(_gray2, (_x * _tileSize, _y * _tileSize))
self.backgroundOriginal.blit(self._gray2, (_x * self.tileSize, _y * self.tileSize))
self.background = pg.transform.scale(self.backgroundOriginal, self.canvas.size)

def SetCanvasPosition(self, x, y):
Expand All @@ -118,7 +59,8 @@ def Magnify(self, mag, pivot):
if self.canvas.collidepoint(*pivot):
p_x, p_y = pivot
else:
p_x, p_y = self.canvas.center
p_x = max(min(pivot[0], self.canvas.x + self.canvas.w), self.canvas.x)
p_y = max(min(pivot[1], self.canvas.y + self.canvas.h), self.canvas.y)
dx = (self.canvas.x - p_x) / self.magnification
dy = (self.canvas.y - p_y) / self.magnification
self.magnification += mag
Expand Down Expand Up @@ -178,6 +120,7 @@ def PositionToPixel(self, x, y) -> (int, int, bool):
return _pixelX, _pixelY, _valid

def OnMouseDown(self, button, x, y):
x, y = self.LocalPosition((x, y))
if button == 1:
pass
# _localX, _localY = self.LocalPosition((x, y))
Expand All @@ -186,14 +129,16 @@ def OnMouseDown(self, button, x, y):
# Brush.OnMouseDown((_pixelX, _pixelY))
# self.Changed()
elif button == 4:
self.Magnify(1, self.LocalPosition((x, y)))
self.Magnify(1, (x, y))
elif button == 5:
self.Magnify(-1, self.LocalPosition((x, y)))
self.Magnify(-1, (x, y))

def OnMouseDrag(self, button, x, y, _x, _y):
x, y = self.LocalPosition((x, y))
_x, _y = self.LocalPosition((_x, _y))
if button == 1:
_pixelX, _pixelY, _valid = self.PositionToPixel(*self.LocalPosition((x, y)))
_prePixelX, _prePixelY, _preValid = self.PositionToPixel(*self.LocalPosition((_x, _y)))
_pixelX, _pixelY, _valid = self.PositionToPixel(x, y)
_prePixelX, _prePixelY, _preValid = self.PositionToPixel(_x, _y)
if _valid and _preValid:
if abs(_prePixelX - _pixelX) > 1 or abs(_prePixelY - _pixelY) > 1:
Brush.DrawLine(_prePixelX, _prePixelY, _pixelX, _pixelY)
Expand All @@ -203,62 +148,3 @@ def OnMouseDrag(self, button, x, y, _x, _y):
elif button == 2:
self.MoveCanvas(x - _x, y - _y)


class PaletteSection(Section):
bgColor = (43, 43, 43)


class FrameSection(Section):
bgColor = (32, 32, 32)



R, G, B = 0, 1, 2
H, S, V = 0, 1, 2

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

bgColor = (43, 43, 43)
colorWheelImage = pg.image.load('data/hue.png')
radius = colorWheelImage.get_width() // 2
upperTerm = 15
colorRGB = (190, 49, 83)
colorHSV = utility.RGB2HSV(colorRGB)
print(colorRGB, colorHSV)
dotImage = pg.image.load('data/dot.png')
dotRadius = dotImage.get_width() // 2

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

def Update(self):
self.surface.fill(self.bgColor)
self.surface.blit(self.colorWheelImage, (self.centerX - self.radius, self.upperTerm))
self.DrawColor()

def SetColor(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()

def DrawColor(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(_theta, _x, _y)
self.surface.blit(self.colorWheelImage, (self.centerX - self.radius, self.upperTerm))
self.surface.blit(self.dotImage,
(self.wheelCenterX + _x - self.dotRadius, self.wheelCenterY + _y - self.dotRadius))

CanvasSection = CanvasSection()
PaletteSection = PaletteSection()
FrameSection = FrameSection()
ColorSection = ColorSection()
Empty = Section()
89 changes: 89 additions & 0 deletions src/Section/ColorSection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import pygame as pg
from src.Section._Section import Section
from src.Brush import Brush
from src import utility
from math import sin, cos, atan2, degrees, radians, sqrt


R, G, B = 0, 1, 2
H, S, V = 0, 1, 2

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

bgColor = (43, 43, 43)
colorWheelImage = pg.image.load('data/hue4.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

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

def Update(self):
self.surface.fill(self.bgColor)
self.DrawColor()

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)

def SetColorHSV(self, color: (float, float, float)):
self.colorHSV = color
self.colorRGB = utility.HSV2RGB(color)
self.Changed()
Brush.pencil.SetCurrentColor(self.colorRGB)

def DrawColor(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.surface.blit(self.dotImage,
(self.wheelCenterX + _x - self.dotRadius,
self.wheelCenterY + _y - self.dotRadius))

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

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

def OnMouseDrag(self, button, x, y, _x, _y):
x, y = self.LocalPosition((x, y))
if self.colorChange:
self.SetColorHSV(self.Position2HSV(x, y))

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


6 changes: 6 additions & 0 deletions src/Section/FrameSection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pygame as pg
from src.Section._Section import Section


class FrameSection(Section):
bgColor = (32, 32, 32)
7 changes: 7 additions & 0 deletions src/Section/PaletteSection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pygame as pg
from src.Section._Section import Section


class PaletteSection(Section):
bgColor = (43, 43, 43)

13 changes: 13 additions & 0 deletions src/Section/Section.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

from src.Section.CanvasSection import CanvasSection
from src.Section.PaletteSection import PaletteSection
from src.Section.FrameSection import FrameSection
from src.Section.ColorSection import ColorSection
from src.Section._Section import Section


CanvasSection = CanvasSection()
PaletteSection = PaletteSection()
FrameSection = FrameSection()
ColorSection = ColorSection()
Empty = Section()
Loading

0 comments on commit fc08e98

Please sign in to comment.