Commit e0ed2fd7 authored by Wolthera van Hövell's avatar Wolthera van Hövell 🛍

Add the ability to let the layer split use color names from a palette.

This also implements two 'closest match' functions in kocolorset. One for the name, the other only for the index.

BUG:348837
parent 73eb12ee
...@@ -137,6 +137,37 @@ qint32 KoColorSet::nColors() ...@@ -137,6 +137,37 @@ qint32 KoColorSet::nColors()
return m_colors.count(); return m_colors.count();
} }
qint32 KoColorSet::getIndexClosestColor(KoColor color, bool useGivenColorSpace)
{
qint32 closestIndex = 0;
quint8 highestPercentage = 0;
quint8 testPercentage = 0;
KoColor compare = color;
for (qint32 i=0; i<nColors(); i++) {
KoColor entry = m_colors.at(i).color;
if (useGivenColorSpace==true && compare.colorSpace()!=entry.colorSpace()) {
entry.convertTo(compare.colorSpace());
} else if(compare.colorSpace()!=entry.colorSpace()) {
compare.convertTo(entry.colorSpace());
}
testPercentage = (255 - compare.colorSpace()->difference(compare.data(), entry.data()));
if (testPercentage>highestPercentage)
{
closestIndex = i;
highestPercentage = testPercentage;
}
}
return closestIndex;
}
QString KoColorSet::closestColorName(KoColor color, bool useGivenColorSpace)
{
int i = getIndexClosestColor(color, useGivenColorSpace);
QString name = m_colors.at(i).name;
return name;
}
bool KoColorSet::saveToDevice(QIODevice *dev) const bool KoColorSet::saveToDevice(QIODevice *dev) const
{ {
QTextStream stream(dev); QTextStream stream(dev);
......
...@@ -85,6 +85,26 @@ public: ...@@ -85,6 +85,26 @@ public:
KoColorSetEntry getColor(quint32 index); KoColorSetEntry getColor(quint32 index);
qint32 nColors(); qint32 nColors();
/**
* @brief getIndexClosestColor
* function that matches the color to all colors in the colorset, and returns the index
* of the closest match.
* @param color the color you wish to compare.
* @param useGivenColorSpace whether to use the color space of the color given
* when the two colors' colorspaces don't match. Else it'll use the entry's colorspace.
* @return returns the int of the closest match.
*/
qint32 getIndexClosestColor(KoColor color, bool useGivenColorSpace = true);
/**
* @brief closestColorName
* convenience function to get the name of the closest match.
* @param color
* @param useGivenColorSpace
* @return
*/
QString closestColorName(KoColor color, bool useGivenColorSpace = true);
private: private:
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#include "dlg_layersplit.h" #include "dlg_layersplit.h"
#include <klocalizedstring.h> #include <klocalizedstring.h>
#include <KoResourceServerProvider.h>
#include <kis_debug.h> #include <kis_debug.h>
#include <KisViewManager.h> #include <KisViewManager.h>
...@@ -45,6 +47,11 @@ DlgLayerSplit::DlgLayerSplit() ...@@ -45,6 +47,11 @@ DlgLayerSplit::DlgLayerSplit()
m_page->intFuzziness->setSingleStep(1); m_page->intFuzziness->setSingleStep(1);
m_colorSetChooser = new KisColorsetChooser();
m_page->paletteChooser->setPopupWidget(m_colorSetChooser);
connect(m_colorSetChooser, SIGNAL(paletteSelected(KoColorSet*)), this, SLOT(slotSetPalette(KoColorSet*)));
KisConfig cfg; KisConfig cfg;
m_page->intFuzziness->setValue(cfg.readEntry<int>("layersplit/fuzziness", 20)); m_page->intFuzziness->setValue(cfg.readEntry<int>("layersplit/fuzziness", 20));
m_page->chkCreateGroupLayer->setChecked(cfg.readEntry<bool>("layerspit/createmastergroup", true)); m_page->chkCreateGroupLayer->setChecked(cfg.readEntry<bool>("layerspit/createmastergroup", true));
...@@ -54,6 +61,17 @@ DlgLayerSplit::DlgLayerSplit() ...@@ -54,6 +61,17 @@ DlgLayerSplit::DlgLayerSplit()
m_page->chkSortLayers->setChecked(cfg.readEntry<bool>("layerspit/sortlayers", true)); m_page->chkSortLayers->setChecked(cfg.readEntry<bool>("layerspit/sortlayers", true));
m_page->chkDisregardOpacity->setChecked(cfg.readEntry<bool>("layerspit/disregardopacity", true)); m_page->chkDisregardOpacity->setChecked(cfg.readEntry<bool>("layerspit/disregardopacity", true));
QString paletteName = cfg.readEntry<QString>("layersplit/paletteName", "Default");
KoResourceServer<KoColorSet> *pserver = KoResourceServerProvider::instance()->paletteServer(false);
KoColorSet *pal = pserver->resourceByName(paletteName);
if (pal) {
m_palette = pal;
m_page->paletteChooser->setText(pal->name());
QIcon icon(QPixmap::fromImage(pal->image()));
m_page->paletteChooser->setIcon(icon);
}
connect(this, SIGNAL(applyClicked()), this, SLOT(applyClicked())); connect(this, SIGNAL(applyClicked()), this, SLOT(applyClicked()));
setMainWidget(m_page); setMainWidget(m_page);
...@@ -73,6 +91,7 @@ void DlgLayerSplit::applyClicked() ...@@ -73,6 +91,7 @@ void DlgLayerSplit::applyClicked()
cfg.writeEntry("layerspit/hideoriginal", m_page->chkHideOriginal->isChecked()); cfg.writeEntry("layerspit/hideoriginal", m_page->chkHideOriginal->isChecked());
cfg.writeEntry("layerspit/sortlayers", m_page->chkSortLayers->isChecked()); cfg.writeEntry("layerspit/sortlayers", m_page->chkSortLayers->isChecked());
cfg.writeEntry("layerspit/disregardopacity", m_page->chkDisregardOpacity->isChecked()); cfg.writeEntry("layerspit/disregardopacity", m_page->chkDisregardOpacity->isChecked());
cfg.writeEntry("layersplit/paletteName", m_palette->name());
accept(); accept();
} }
...@@ -113,5 +132,17 @@ int DlgLayerSplit::fuzziness() const ...@@ -113,5 +132,17 @@ int DlgLayerSplit::fuzziness() const
} }
KoColorSet *DlgLayerSplit::palette() const
{
return m_palette;
}
void DlgLayerSplit::slotSetPalette(KoColorSet *pal)
{
if (pal) {
m_palette = pal;
m_page->paletteChooser->setText(pal->name());
QIcon icon(QPixmap::fromImage(pal->image()));
m_page->paletteChooser->setIcon(icon);
}
}
...@@ -20,7 +20,8 @@ ...@@ -20,7 +20,8 @@
#define DLG_LAYERSPLIT #define DLG_LAYERSPLIT
#include <KoDialog.h> #include <KoDialog.h>
#include <KoColorSet.h>
#include <KisColorsetChooser.h>
#include <kis_types.h> #include <kis_types.h>
#include "wdg_layersplit.h" #include "wdg_layersplit.h"
...@@ -46,13 +47,17 @@ public: ...@@ -46,13 +47,17 @@ public:
bool sortLayers() const; bool sortLayers() const;
bool disregardOpacity() const; bool disregardOpacity() const;
int fuzziness() const; int fuzziness() const;
KoColorSet* palette() const;
private Q_SLOTS: private Q_SLOTS:
void applyClicked(); void applyClicked();
void slotSetPalette(KoColorSet *pal);
private: private:
WdgLayerSplit* m_page; WdgLayerSplit* m_page;
KisColorsetChooser *m_colorSetChooser;
KoColorSet *m_palette;
}; };
#endif // DLG_LAYERSPLIT #endif // DLG_LAYERSPLIT
...@@ -149,9 +149,13 @@ void LayerSplit::slotLayerSplit() ...@@ -149,9 +149,13 @@ void LayerSplit::slotLayerSplit()
} }
if (!found) { if (!found) {
QString name = dlg.palette()->closestColorName(c);
if (name.toLower() == "untitled" || name.toLower() == "none" || name.toLower() == "") {
name = KoColor::toQString(c);
}
Layer l; Layer l;
l.color = c; l.color = c;
l.device = new KisPaintDevice(cs, KoColor::toQString(c)); l.device = new KisPaintDevice(cs, name);
l.accessor = l.device->createRandomAccessorNG(col, row); l.accessor = l.device->createRandomAccessorNG(col, row);
l.accessor->moveTo(col, row); l.accessor->moveTo(col, row);
memcpy(l.accessor->rawData(), acc->rawDataConst(), cs->pixelSize()); memcpy(l.accessor->rawData(), acc->rawDataConst(), cs->pixelSize());
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>449</width> <width>449</width>
<height>271</height> <height>273</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
...@@ -77,7 +77,7 @@ ...@@ -77,7 +77,7 @@
</widget> </widget>
</item> </item>
<item row="6" column="1"> <item row="6" column="1">
<widget class="KisSliderSpinBox" name="intFuzziness"/> <widget class="KisSliderSpinBox" name="intFuzziness" native="true"/>
</item> </item>
<item row="5" column="0"> <item row="5" column="0">
<widget class="QCheckBox" name="chkDisregardOpacity"> <widget class="QCheckBox" name="chkDisregardOpacity">
...@@ -86,6 +86,20 @@ ...@@ -86,6 +86,20 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="7" column="0" colspan="2">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Palette to use for naming the layers:</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<widget class="KisPopupButton" name="paletteChooser">
<property name="text">
<string>Choose Palette</string>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
<item> <item>
...@@ -123,6 +137,11 @@ ...@@ -123,6 +137,11 @@
</layout> </layout>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget>
<class>KisPopupButton</class>
<extends>QPushButton</extends>
<header>kis_popup_button.h</header>
</customwidget>
<customwidget> <customwidget>
<class>KisSliderSpinBox</class> <class>KisSliderSpinBox</class>
<extends>QWidget</extends> <extends>QWidget</extends>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment