Skip to content

Commit b059a32

Browse files
committed
Fixed CMAP labels and refactored unmaintainable match pattern.
1 parent 19f07fb commit b059a32

File tree

3 files changed

+78
-156
lines changed

3 files changed

+78
-156
lines changed

imagecolorpicker/gradienteditor.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ def __init__(
3434
self._gradientModel.load(self._allColorMaps, self._gradient)
3535
self.tableView.resizeColumnsToContents()
3636
self.tableView.doubleClicked.connect(self.doubleClicked.emit)
37+
38+
self.descriptionLayout: QVBoxLayout
39+
self._labels: List[QLabel] = []
40+
for weight, mix, _ in self._allColorMaps:
41+
label: QLabel = QLabel(f"{GradientWeight(weight).name} -> {GradientMix(mix).name}")
42+
self._labels.append(label)
43+
44+
for label in reversed(self._labels):
45+
self.descriptionLayout.addWidget(label)
46+
self.update()

imagecolorpicker/gradienteditor.ui

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>386</width>
10-
<height>300</height>
10+
<height>339</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -35,94 +35,10 @@
3535
<number>12</number>
3636
</property>
3737
<item>
38-
<layout class="QVBoxLayout" name="verticalLayout">
38+
<layout class="QVBoxLayout" name="descriptionLayout">
3939
<property name="spacing">
4040
<number>6</number>
4141
</property>
42-
<item>
43-
<widget class="QLabel" name="label">
44-
<property name="text">
45-
<string>Lin/RGB</string>
46-
</property>
47-
</widget>
48-
</item>
49-
<item>
50-
<widget class="QLabel" name="label_2">
51-
<property name="text">
52-
<string>Lin/Oklab</string>
53-
</property>
54-
</widget>
55-
</item>
56-
<item>
57-
<widget class="QLabel" name="label_7">
58-
<property name="text">
59-
<string>Lin/Cielab</string>
60-
</property>
61-
</widget>
62-
</item>
63-
<item>
64-
<widget class="QLabel" name="label_3">
65-
<property name="text">
66-
<string>RGB/RGB</string>
67-
</property>
68-
</widget>
69-
</item>
70-
<item>
71-
<widget class="QLabel" name="label_4">
72-
<property name="text">
73-
<string>RGB/Oklab</string>
74-
</property>
75-
</widget>
76-
</item>
77-
<item>
78-
<widget class="QLabel" name="label_8">
79-
<property name="text">
80-
<string>RGB/Cielab</string>
81-
</property>
82-
</widget>
83-
</item>
84-
<item>
85-
<widget class="QLabel" name="label_5">
86-
<property name="text">
87-
<string>Oklab/RGB</string>
88-
</property>
89-
</widget>
90-
</item>
91-
<item>
92-
<widget class="QLabel" name="label_6">
93-
<property name="text">
94-
<string>Oklab/Oklab</string>
95-
</property>
96-
</widget>
97-
</item>
98-
<item>
99-
<widget class="QLabel" name="label_9">
100-
<property name="text">
101-
<string>Oklab/Cielab</string>
102-
</property>
103-
</widget>
104-
</item>
105-
<item>
106-
<widget class="QLabel" name="label_10">
107-
<property name="text">
108-
<string>Cielab/RGB</string>
109-
</property>
110-
</widget>
111-
</item>
112-
<item>
113-
<widget class="QLabel" name="label_11">
114-
<property name="text">
115-
<string>Cielab/Oklab</string>
116-
</property>
117-
</widget>
118-
</item>
119-
<item>
120-
<widget class="QLabel" name="label_12">
121-
<property name="text">
122-
<string>Cielab/Cielab</string>
123-
</property>
124-
</widget>
125-
</item>
12642
</layout>
12743
</item>
12844
<item>

imagecolorpicker/mainwindow.py

Lines changed: 66 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -180,76 +180,72 @@ def quit(self: Self) -> None:
180180

181181
def copy(self: Self) -> None:
182182
clipboard = QGuiApplication.clipboard()
183-
184-
match self.languageDropDown.currentData():
185-
case Language.GLSL:
186-
match self.representationDropDown.currentData():
187-
case Representation.ColorMap:
188-
clipboard.setText(self.gradientEditor._gradient.buildColorMap(self.weightDropDown.currentData(), self.mixDropDown.currentData()))
189-
case Representation.Picked3ComponentColor:
190-
clipboard.setText('vec3({:.2f}, {:.2f}, {:.2f})'.format(*self.picker.components))
191-
case Representation.Picked4ComponentColor:
192-
clipboard.setText('vec4({:.2f}, {:.2f}, {:.2f}, 1)'.format(*self.picker.components))
193-
case Representation.PickedNearestGradientWeight:
194-
for weight, mix, colorMap in self.gradientEditor._allColorMaps:
195-
if weight == self.weightDropDown.currentData() and mix == self.mixDropDown.currentData():
196-
clipboard.setText('{:.2f}'.format(self.gradientEditor._gradient.nearestWeightInColorMap(colorMap, vec3(*self.picker.components))))
197-
case Representation.GradientColorArray:
198-
clipboard.setText('vec3[{}]({})'.format(
199-
len(self.gradientEditor._gradient._colors),
200-
', '.join(map(
201-
str,
202-
self.gradientEditor._gradient._colors,
203-
)),
204-
))
205-
case Representation.GradientWeightArray:
206-
weights = self.gradientEditor._gradient.determineWeights(self.weightDropDown.currentData())
207-
clipboard.setText('float[{}]({})'.format(
208-
len(weights),
209-
', '.join(map(
210-
lambda weight: '{:.2f}'.format(weight),
211-
weights,
212-
)),
213-
))
214-
case Language.HLSL:
215-
match self.representationDropDown.currentData():
216-
case Representation.ColorMap:
217-
clipboard.setText(self.gradientEditor._gradient.buildColorMap(self.weightDropDown.currentData(), self.mixDropDown.currentData()).replace('vec3', 'float3'))
218-
case Representation.Picked3ComponentColor:
219-
clipboard.setText('float3({:.2f}, {:.2f}, {:.2f})'.format(*self.picker.components))
220-
case Representation.Picked4ComponentColor:
221-
clipboard.setText('float4({:.2f}, {:.2f}, {:.2f}, 1)'.format(*self.picker.components))
222-
case Representation.PickedNearestGradientWeight:
223-
for weight, mix, colorMap in self.gradientEditor._allColorMaps:
224-
if weight == self.weightDropDown.currentData() and mix == self.mixDropDown.currentData():
225-
clipboard.setText('{:.2f}'.format(self.gradientEditor._gradient.nearestWeightInColorMap(colorMap, vec3(*self.picker.components))))
226-
case Representation.GradientColorArray:
227-
clipboard.setText('{{{}}}'.format(
228-
', '.join(map(
229-
lambda color: ', '.join(map(lambda component: '{:.2f}'.format(component), color._color.to_tuple())),
230-
self.gradientEditor._gradient._colors,
231-
)),
232-
))
233-
case Representation.GradientWeightArray:
234-
weights = self.gradientEditor._gradient.determineWeights(self.weightDropDown.currentData())
235-
clipboard.setText('{{{}}}'.format(
236-
', '.join(map(
237-
lambda weight: '{:.2f}'.format(weight),
238-
weights,
239-
)),
240-
))
241-
case Language.CSS:
242-
match self.representationDropDown.currentData():
243-
case Representation.ColorMap:
244-
clipboard.setText(self.gradientEditor._gradient.buildCSSGradient(self.weightDropDown.currentData(), self.mixDropDown.currentData()))
245-
case Representation.Picked3ComponentColor:
246-
clipboard.setText(self.picker._color.name())
247-
case Language.SVG:
248-
match self.representationDropDown.currentData():
249-
case Representation.ColorMap:
250-
clipboard.setText(self.gradientEditor._gradient.buildSVGGradient(self.weightDropDown.currentData(), self.mixDropDown.currentData()))
251-
case Representation.Picked3ComponentColor:
252-
clipboard.setText(self.picker._color.name())
183+
currentLanguage: Language = self.languageDropDown.currentData()
184+
currentRepresentation: Representation = self.representationDropDown.currentData()
185+
if currentLanguage == Language.GLSL:
186+
if currentRepresentation == Representation.ColorMap:
187+
clipboard.setText(self.gradientEditor._gradient.buildColorMap(self.weightDropDown.currentData(), self.mixDropDown.currentData()))
188+
elif currentRepresentation == Representation.Picked3ComponentColor:
189+
clipboard.setText('vec3({:.2f}, {:.2f}, {:.2f})'.format(*self.picker.components))
190+
elif currentRepresentation == Representation.Picked4ComponentColor:
191+
clipboard.setText('vec4({:.2f}, {:.2f}, {:.2f}, 1)'.format(*self.picker.components))
192+
elif currentRepresentation == Representation.PickedNearestGradientWeight:
193+
for weight, mix, colorMap in self.gradientEditor._allColorMaps:
194+
if weight == self.weightDropDown.currentData() and mix == self.mixDropDown.currentData():
195+
clipboard.setText('{:.2f}'.format(self.gradientEditor._gradient.nearestWeightInColorMap(colorMap, vec3(*self.picker.components))))
196+
elif currentRepresentation == Representation.GradientColorArray:
197+
clipboard.setText('vec3[{}]({})'.format(
198+
len(self.gradientEditor._gradient._colors),
199+
', '.join(map(
200+
str,
201+
self.gradientEditor._gradient._colors,
202+
)),
203+
))
204+
elif currentRepresentation == Representation.GradientWeightArray:
205+
weights = self.gradientEditor._gradient.determineWeights(self.weightDropDown.currentData())
206+
clipboard.setText('float[{}]({})'.format(
207+
len(weights),
208+
', '.join(map(
209+
lambda weight: '{:.2f}'.format(weight),
210+
weights,
211+
)),
212+
))
213+
elif currentLanguage == Language.HLSL:
214+
if currentRepresentation == Representation.ColorMap:
215+
clipboard.setText(self.gradientEditor._gradient.buildColorMap(self.weightDropDown.currentData(), self.mixDropDown.currentData()).replace('vec3', 'float3'))
216+
elif currentRepresentation == Representation.Picked3ComponentColor:
217+
clipboard.setText('float3({:.2f}, {:.2f}, {:.2f})'.format(*self.picker.components))
218+
elif currentRepresentation == Representation.Picked4ComponentColor:
219+
clipboard.setText('float4({:.2f}, {:.2f}, {:.2f}, 1)'.format(*self.picker.components))
220+
elif currentRepresentation == Representation.PickedNearestGradientWeight:
221+
for weight, mix, colorMap in self.gradientEditor._allColorMaps:
222+
if weight == self.weightDropDown.currentData() and mix == self.mixDropDown.currentData():
223+
clipboard.setText('{:.2f}'.format(self.gradientEditor._gradient.nearestWeightInColorMap(colorMap, vec3(*self.picker.components))))
224+
elif currentRepresentation == Representation.GradientColorArray:
225+
clipboard.setText('{{{}}}'.format(
226+
', '.join(map(
227+
lambda color: ', '.join(map(lambda component: '{:.2f}'.format(component), color._color.to_tuple())),
228+
self.gradientEditor._gradient._colors,
229+
)),
230+
))
231+
elif currentRepresentation == Representation.GradientWeightArray:
232+
weights = self.gradientEditor._gradient.determineWeights(self.weightDropDown.currentData())
233+
clipboard.setText('{{{}}}'.format(
234+
', '.join(map(
235+
lambda weight: '{:.2f}'.format(weight),
236+
weights,
237+
)),
238+
))
239+
elif currentLanguage == Language.CSS:
240+
if currentRepresentation == Representation.ColorMap:
241+
clipboard.setText(self.gradientEditor._gradient.buildCSSGradient(self.weightDropDown.currentData(), self.mixDropDown.currentData()))
242+
elif currentRepresentation == Representation.Picked3ComponentColor:
243+
clipboard.setText(self.picker._color.name())
244+
elif currentLanguage == Language.SVG:
245+
if currentRepresentation == Representation.ColorMap:
246+
clipboard.setText(self.gradientEditor._gradient.buildSVGGradient(self.weightDropDown.currentData(), self.mixDropDown.currentData()))
247+
elif currentRepresentation == Representation.Picked3ComponentColor:
248+
clipboard.setText(self.picker._color.name())
253249

254250
def paste(self: Self) -> None:
255251
clipboard = QGuiApplication.clipboard()

0 commit comments

Comments
 (0)