This repository has been archived on 2020-08-22. You can view files and clone it, but cannot push or open issues or pull requests.
pyaudviz/src/components/color.py

208 lines
7.5 KiB
Python
Raw Normal View History

from PIL import Image, ImageDraw
2017-06-24 23:12:41 -04:00
from PyQt5 import QtGui, QtCore, QtWidgets
2017-06-23 18:38:05 -04:00
from PyQt5.QtGui import QColor
from PIL.ImageQt import ImageQt
import os
2017-07-02 20:46:48 -04:00
from component import Component
from toolkit.frame import BlankFrame, FloodFrame, FramePainter, PaintColor
from toolkit import rgbFromString, pickColor
2017-06-06 11:14:39 -04:00
2017-07-02 20:46:48 -04:00
class Component(Component):
name = 'Color'
version = '1.0.0'
def widget(self, *args):
2017-06-06 11:14:39 -04:00
self.color1 = (0, 0, 0)
self.color2 = (133, 133, 133)
self.x = 0
self.y = 0
super().widget(*args)
2017-06-06 11:14:39 -04:00
self.page.lineEdit_color1.setText('%s,%s,%s' % self.color1)
self.page.lineEdit_color2.setText('%s,%s,%s' % self.color2)
2017-06-06 11:14:39 -04:00
btnStyle1 = "QPushButton { background-color : %s; outline: none; }" \
2017-06-06 11:14:39 -04:00
% QColor(*self.color1).name()
btnStyle2 = "QPushButton { background-color : %s; outline: none; }" \
2017-06-06 11:14:39 -04:00
% QColor(*self.color2).name()
self.page.pushButton_color1.setStyleSheet(btnStyle1)
self.page.pushButton_color2.setStyleSheet(btnStyle2)
self.page.pushButton_color1.clicked.connect(lambda: self.pickColor(1))
self.page.pushButton_color2.clicked.connect(lambda: self.pickColor(2))
2017-06-06 11:14:39 -04:00
# disable color #2 until non-default 'fill' option gets changed
self.page.lineEdit_color2.setDisabled(True)
self.page.pushButton_color2.setDisabled(True)
self.page.spinBox_width.setValue(
2017-07-13 00:05:11 -04:00
int(self.settings.value("outputWidth")))
self.page.spinBox_height.setValue(
2017-07-13 00:05:11 -04:00
int(self.settings.value("outputHeight")))
2017-06-06 11:14:39 -04:00
2017-06-23 23:00:24 -04:00
self.fillLabels = [
'Solid',
'Linear Gradient',
'Radial Gradient',
]
for label in self.fillLabels:
self.page.comboBox_fill.addItem(label)
self.page.comboBox_fill.setCurrentIndex(0)
self.trackWidgets(
{
'x': self.page.spinBox_x,
'y': self.page.spinBox_y,
'sizeWidth': self.page.spinBox_width,
'sizeHeight': self.page.spinBox_height,
'trans': self.page.checkBox_trans,
'spread': self.page.comboBox_spread,
'stretch': self.page.checkBox_stretch,
'RG_start': self.page.spinBox_radialGradient_start,
'LG_start': self.page.spinBox_linearGradient_start,
'RG_end': self.page.spinBox_radialGradient_end,
'LG_end': self.page.spinBox_linearGradient_end,
'RG_centre': self.page.spinBox_radialGradient_spread,
'fillType': self.page.comboBox_fill,
}, presetNames={
'sizeWidth': 'width',
'sizeHeight': 'height',
}
)
def update(self):
self.color1 = rgbFromString(self.page.lineEdit_color1.text())
self.color2 = rgbFromString(self.page.lineEdit_color2.text())
fillType = self.page.comboBox_fill.currentIndex()
if fillType == 0:
self.page.lineEdit_color2.setEnabled(False)
self.page.pushButton_color2.setEnabled(False)
self.page.checkBox_trans.setEnabled(False)
self.page.checkBox_stretch.setEnabled(False)
self.page.comboBox_spread.setEnabled(False)
else:
self.page.lineEdit_color2.setEnabled(True)
self.page.pushButton_color2.setEnabled(True)
self.page.checkBox_trans.setEnabled(True)
self.page.checkBox_stretch.setEnabled(True)
self.page.comboBox_spread.setEnabled(True)
if self.page.checkBox_trans.isChecked():
self.page.lineEdit_color2.setEnabled(False)
self.page.pushButton_color2.setEnabled(False)
self.page.fillWidget.setCurrentIndex(fillType)
super().update()
2017-06-06 11:14:39 -04:00
def previewRender(self, previewWorker):
2017-07-13 00:05:11 -04:00
width = int(self.settings.value('outputWidth'))
height = int(self.settings.value('outputHeight'))
return self.drawFrame(width, height)
2017-06-06 11:14:39 -04:00
def properties(self):
return ['static']
2017-06-06 11:14:39 -04:00
def frameRender(self, layerNo, frameNo):
2017-07-13 00:05:11 -04:00
width = int(self.settings.value('outputWidth'))
height = int(self.settings.value('outputHeight'))
return self.drawFrame(width, height)
2017-06-06 11:14:39 -04:00
def drawFrame(self, width, height):
2017-06-06 11:14:39 -04:00
r, g, b = self.color1
shapeSize = (self.sizeWidth, self.sizeHeight)
# in default state, skip all this logic and return a plain fill
2017-06-23 23:00:24 -04:00
if self.fillType == 0 and shapeSize == (width, height) \
and self.x == 0 and self.y == 0:
return FloodFrame(width, height, (r, g, b, 255))
# Return a solid image at x, y
if self.fillType == 0:
frame = BlankFrame(width, height)
image = Image.new("RGBA", shapeSize, (r, g, b, 255))
frame.paste(image, box=(self.x, self.y))
return frame
# Now fills that require using Qt...
elif self.fillType > 0:
image = FramePainter(width, height)
if self.stretch:
2017-06-23 23:00:24 -04:00
w = width
h = height
else:
2017-06-23 23:00:24 -04:00
w = self.sizeWidth
h = self.sizeWidth
if self.fillType == 1: # Linear Gradient
brush = QtGui.QLinearGradient(
self.LG_start,
2017-06-16 20:43:40 -04:00
self.LG_start,
self.LG_end+width/3,
2017-06-16 20:43:40 -04:00
self.LG_end)
elif self.fillType == 2: # Radial Gradient
brush = QtGui.QRadialGradient(
self.RG_start,
self.RG_end,
w, h,
2017-06-16 20:43:40 -04:00
self.RG_centre)
2017-06-16 20:43:40 -04:00
brush.setSpread(self.spread)
brush.setColorAt(0.0, PaintColor(*self.color1))
if self.trans:
brush.setColorAt(1.0, PaintColor(0, 0, 0, 0))
2017-06-16 20:43:40 -04:00
elif self.fillType == 1 and self.stretch:
brush.setColorAt(0.2, PaintColor(*self.color2))
else:
brush.setColorAt(1.0, PaintColor(*self.color2))
image.setBrush(brush)
image.drawRect(
2017-06-23 23:00:24 -04:00
self.x, self.y,
2017-06-23 17:14:39 -04:00
self.sizeWidth, self.sizeHeight
)
return image.finalize()
def loadPreset(self, pr, *args):
super().loadPreset(pr, *args)
2017-06-03 22:58:40 -04:00
self.page.lineEdit_color1.setText('%s,%s,%s' % pr['color1'])
self.page.lineEdit_color2.setText('%s,%s,%s' % pr['color2'])
2017-06-06 11:14:39 -04:00
btnStyle1 = "QPushButton { background-color : %s; outline: none; }" \
2017-06-06 11:14:39 -04:00
% QColor(*pr['color1']).name()
btnStyle2 = "QPushButton { background-color : %s; outline: none; }" \
2017-06-06 11:14:39 -04:00
% QColor(*pr['color2']).name()
self.page.pushButton_color1.setStyleSheet(btnStyle1)
self.page.pushButton_color2.setStyleSheet(btnStyle2)
2017-06-06 11:14:39 -04:00
def savePreset(self):
saveValueStore = super().savePreset()
saveValueStore['color1'] = self.color1
saveValueStore['color2'] = self.color2
return saveValueStore
2017-06-06 11:14:39 -04:00
def pickColor(self, num):
RGBstring, btnStyle = pickColor()
if not RGBstring:
return
if num == 1:
self.page.lineEdit_color1.setText(RGBstring)
self.page.pushButton_color1.setStyleSheet(btnStyle)
else:
self.page.lineEdit_color2.setText(RGBstring)
self.page.pushButton_color2.setStyleSheet(btnStyle)
def commandHelp(self):
print('Specify a color:\n color=255,255,255')
def command(self, arg):
if '=' in arg:
key, arg = arg.split('=', 1)
if key == 'color':
self.page.lineEdit_color1.setText(arg)
return
super().command(arg)