Skip to content

Commit

Permalink
Fixed CMAP labels and refactored unmaintainable match pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeStahL committed Nov 21, 2024
1 parent 19f07fb commit b059a32
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 156 deletions.
10 changes: 10 additions & 0 deletions imagecolorpicker/gradienteditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ def __init__(
self._gradientModel.load(self._allColorMaps, self._gradient)
self.tableView.resizeColumnsToContents()
self.tableView.doubleClicked.connect(self.doubleClicked.emit)

self.descriptionLayout: QVBoxLayout
self._labels: List[QLabel] = []
for weight, mix, _ in self._allColorMaps:
label: QLabel = QLabel(f"{GradientWeight(weight).name} -> {GradientMix(mix).name}")
self._labels.append(label)

for label in reversed(self._labels):
self.descriptionLayout.addWidget(label)
self.update()
88 changes: 2 additions & 86 deletions imagecolorpicker/gradienteditor.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>386</width>
<height>300</height>
<height>339</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -35,94 +35,10 @@
<number>12</number>
</property>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<layout class="QVBoxLayout" name="descriptionLayout">
<property name="spacing">
<number>6</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Lin/RGB</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Lin/Oklab</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string>Lin/Cielab</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>RGB/RGB</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>RGB/Oklab</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_8">
<property name="text">
<string>RGB/Cielab</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Oklab/RGB</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Oklab/Oklab</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_9">
<property name="text">
<string>Oklab/Cielab</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_10">
<property name="text">
<string>Cielab/RGB</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_11">
<property name="text">
<string>Cielab/Oklab</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_12">
<property name="text">
<string>Cielab/Cielab</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down
136 changes: 66 additions & 70 deletions imagecolorpicker/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,76 +180,72 @@ def quit(self: Self) -> None:

def copy(self: Self) -> None:
clipboard = QGuiApplication.clipboard()

match self.languageDropDown.currentData():
case Language.GLSL:
match self.representationDropDown.currentData():
case Representation.ColorMap:
clipboard.setText(self.gradientEditor._gradient.buildColorMap(self.weightDropDown.currentData(), self.mixDropDown.currentData()))
case Representation.Picked3ComponentColor:
clipboard.setText('vec3({:.2f}, {:.2f}, {:.2f})'.format(*self.picker.components))
case Representation.Picked4ComponentColor:
clipboard.setText('vec4({:.2f}, {:.2f}, {:.2f}, 1)'.format(*self.picker.components))
case Representation.PickedNearestGradientWeight:
for weight, mix, colorMap in self.gradientEditor._allColorMaps:
if weight == self.weightDropDown.currentData() and mix == self.mixDropDown.currentData():
clipboard.setText('{:.2f}'.format(self.gradientEditor._gradient.nearestWeightInColorMap(colorMap, vec3(*self.picker.components))))
case Representation.GradientColorArray:
clipboard.setText('vec3[{}]({})'.format(
len(self.gradientEditor._gradient._colors),
', '.join(map(
str,
self.gradientEditor._gradient._colors,
)),
))
case Representation.GradientWeightArray:
weights = self.gradientEditor._gradient.determineWeights(self.weightDropDown.currentData())
clipboard.setText('float[{}]({})'.format(
len(weights),
', '.join(map(
lambda weight: '{:.2f}'.format(weight),
weights,
)),
))
case Language.HLSL:
match self.representationDropDown.currentData():
case Representation.ColorMap:
clipboard.setText(self.gradientEditor._gradient.buildColorMap(self.weightDropDown.currentData(), self.mixDropDown.currentData()).replace('vec3', 'float3'))
case Representation.Picked3ComponentColor:
clipboard.setText('float3({:.2f}, {:.2f}, {:.2f})'.format(*self.picker.components))
case Representation.Picked4ComponentColor:
clipboard.setText('float4({:.2f}, {:.2f}, {:.2f}, 1)'.format(*self.picker.components))
case Representation.PickedNearestGradientWeight:
for weight, mix, colorMap in self.gradientEditor._allColorMaps:
if weight == self.weightDropDown.currentData() and mix == self.mixDropDown.currentData():
clipboard.setText('{:.2f}'.format(self.gradientEditor._gradient.nearestWeightInColorMap(colorMap, vec3(*self.picker.components))))
case Representation.GradientColorArray:
clipboard.setText('{{{}}}'.format(
', '.join(map(
lambda color: ', '.join(map(lambda component: '{:.2f}'.format(component), color._color.to_tuple())),
self.gradientEditor._gradient._colors,
)),
))
case Representation.GradientWeightArray:
weights = self.gradientEditor._gradient.determineWeights(self.weightDropDown.currentData())
clipboard.setText('{{{}}}'.format(
', '.join(map(
lambda weight: '{:.2f}'.format(weight),
weights,
)),
))
case Language.CSS:
match self.representationDropDown.currentData():
case Representation.ColorMap:
clipboard.setText(self.gradientEditor._gradient.buildCSSGradient(self.weightDropDown.currentData(), self.mixDropDown.currentData()))
case Representation.Picked3ComponentColor:
clipboard.setText(self.picker._color.name())
case Language.SVG:
match self.representationDropDown.currentData():
case Representation.ColorMap:
clipboard.setText(self.gradientEditor._gradient.buildSVGGradient(self.weightDropDown.currentData(), self.mixDropDown.currentData()))
case Representation.Picked3ComponentColor:
clipboard.setText(self.picker._color.name())
currentLanguage: Language = self.languageDropDown.currentData()
currentRepresentation: Representation = self.representationDropDown.currentData()
if currentLanguage == Language.GLSL:
if currentRepresentation == Representation.ColorMap:
clipboard.setText(self.gradientEditor._gradient.buildColorMap(self.weightDropDown.currentData(), self.mixDropDown.currentData()))
elif currentRepresentation == Representation.Picked3ComponentColor:
clipboard.setText('vec3({:.2f}, {:.2f}, {:.2f})'.format(*self.picker.components))
elif currentRepresentation == Representation.Picked4ComponentColor:
clipboard.setText('vec4({:.2f}, {:.2f}, {:.2f}, 1)'.format(*self.picker.components))
elif currentRepresentation == Representation.PickedNearestGradientWeight:
for weight, mix, colorMap in self.gradientEditor._allColorMaps:
if weight == self.weightDropDown.currentData() and mix == self.mixDropDown.currentData():
clipboard.setText('{:.2f}'.format(self.gradientEditor._gradient.nearestWeightInColorMap(colorMap, vec3(*self.picker.components))))
elif currentRepresentation == Representation.GradientColorArray:
clipboard.setText('vec3[{}]({})'.format(
len(self.gradientEditor._gradient._colors),
', '.join(map(
str,
self.gradientEditor._gradient._colors,
)),
))
elif currentRepresentation == Representation.GradientWeightArray:
weights = self.gradientEditor._gradient.determineWeights(self.weightDropDown.currentData())
clipboard.setText('float[{}]({})'.format(
len(weights),
', '.join(map(
lambda weight: '{:.2f}'.format(weight),
weights,
)),
))
elif currentLanguage == Language.HLSL:
if currentRepresentation == Representation.ColorMap:
clipboard.setText(self.gradientEditor._gradient.buildColorMap(self.weightDropDown.currentData(), self.mixDropDown.currentData()).replace('vec3', 'float3'))
elif currentRepresentation == Representation.Picked3ComponentColor:
clipboard.setText('float3({:.2f}, {:.2f}, {:.2f})'.format(*self.picker.components))
elif currentRepresentation == Representation.Picked4ComponentColor:
clipboard.setText('float4({:.2f}, {:.2f}, {:.2f}, 1)'.format(*self.picker.components))
elif currentRepresentation == Representation.PickedNearestGradientWeight:
for weight, mix, colorMap in self.gradientEditor._allColorMaps:
if weight == self.weightDropDown.currentData() and mix == self.mixDropDown.currentData():
clipboard.setText('{:.2f}'.format(self.gradientEditor._gradient.nearestWeightInColorMap(colorMap, vec3(*self.picker.components))))
elif currentRepresentation == Representation.GradientColorArray:
clipboard.setText('{{{}}}'.format(
', '.join(map(
lambda color: ', '.join(map(lambda component: '{:.2f}'.format(component), color._color.to_tuple())),
self.gradientEditor._gradient._colors,
)),
))
elif currentRepresentation == Representation.GradientWeightArray:
weights = self.gradientEditor._gradient.determineWeights(self.weightDropDown.currentData())
clipboard.setText('{{{}}}'.format(
', '.join(map(
lambda weight: '{:.2f}'.format(weight),
weights,
)),
))
elif currentLanguage == Language.CSS:
if currentRepresentation == Representation.ColorMap:
clipboard.setText(self.gradientEditor._gradient.buildCSSGradient(self.weightDropDown.currentData(), self.mixDropDown.currentData()))
elif currentRepresentation == Representation.Picked3ComponentColor:
clipboard.setText(self.picker._color.name())
elif currentLanguage == Language.SVG:
if currentRepresentation == Representation.ColorMap:
clipboard.setText(self.gradientEditor._gradient.buildSVGGradient(self.weightDropDown.currentData(), self.mixDropDown.currentData()))
elif currentRepresentation == Representation.Picked3ComponentColor:
clipboard.setText(self.picker._color.name())

def paste(self: Self) -> None:
clipboard = QGuiApplication.clipboard()
Expand Down

0 comments on commit b059a32

Please sign in to comment.