aboutsummaryrefslogtreecommitdiff
path: root/components/color.py
blob: a86927b852c7f53b50b34e4487aec7d7083bfa80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from PIL import Image, ImageDraw
from PyQt4 import uic, QtGui, QtCore
from PyQt4.QtGui import QColor
import os
from . import __base__


class Component(__base__.Component):
    '''Color'''
    def widget(self, parent):
        self.parent = parent
        page = uic.loadUi(os.path.join(
            os.path.dirname(os.path.realpath(__file__)), 'color.ui'))

        self.color1 = (0, 0, 0)
        self.color2 = (133, 133, 133)
        self.x = 0
        self.y = 0

        page.lineEdit_color1.setText('%s,%s,%s' % self.color1)
        page.lineEdit_color2.setText('%s,%s,%s' % self.color2)

        btnStyle = "QPushButton { background-color : %s; outline: none; }" \
            % QColor(*self.color1).name()

        btnStyle = "QPushButton { background-color : %s; outline: none; }" \
            % QColor(*self.color2).name()

        page.pushButton_color1.setStyleSheet(btnStyle)
        page.pushButton_color2.setStyleSheet(btnStyle)
        page.pushButton_color1.clicked.connect(lambda: self.pickColor(1))
        page.pushButton_color2.clicked.connect(lambda: self.pickColor(2))

        # disable color #2 until non-default 'fill' option gets changed
        page.lineEdit_color2.setDisabled(True)
        page.pushButton_color2.setDisabled(True)
        page.spinBox_x.setValue(self.x)
        page.spinBox_x.setValue(self.y)

        page.lineEdit_color1.textChanged.connect(self.update)
        page.lineEdit_color2.textChanged.connect(self.update)
        page.spinBox_x.valueChanged.connect(self.update)
        page.spinBox_y.valueChanged.connect(self.update)
        self.page = page
        return page

    def update(self):
        self.color1 = self.RGBFromString(self.page.lineEdit_color1.text())
        self.color2 = self.RGBFromString(self.page.lineEdit_color2.text())
        self.x = self.page.spinBox_x.value()
        self.y = self.page.spinBox_y.value()
        self.parent.drawPreview()

    def previewRender(self, previewWorker):
        width = int(previewWorker.core.settings.value('outputWidth'))
        height = int(previewWorker.core.settings.value('outputHeight'))
        return self.drawFrame(width, height)

    def preFrameRender(self, **kwargs):
        super().preFrameRender(**kwargs)
        return ['static']

    def frameRender(self, moduleNo, arrayNo, frameNo):
        width = int(self.worker.core.settings.value('outputWidth'))
        height = int(self.worker.core.settings.value('outputHeight'))
        return self.drawFrame(width, height)

    def drawFrame(self, width, height):
        r, g, b = self.color1
        return Image.new("RGBA", (width, height), (r, g, b, 255))

    def loadPreset(self, pr, presetName=None):
        self.currentPreset = presetName if presetName else pr['preset']
        self.page.lineEdit_color1.setText('%s,%s,%s' % pr['color1'])
        self.page.lineEdit_color2.setText('%s,%s,%s' % pr['color2'])

        btnStyle = "QPushButton { background-color : %s; outline: none; }" \
            % QColor(*pr['color1']).name()

        btnStyle = "QPushButton { background-color : %s; outline: none; }" \
            % QColor(*pr['color2']).name()

        self.page.pushButton_color1.setStyleSheet(btnStyle)
        self.page.pushButton_color2.setStyleSheet(btnStyle)

    def savePreset(self):
        return {
            'preset': self.currentPreset,
            'color1': self.color1,
            'color2': self.color2,
        }

    def pickColor(self, num):
        RGBstring, btnStyle = super().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)