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()
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
{
QTextStream stream(dev);
......
......@@ -85,6 +85,26 @@ public:
KoColorSetEntry getColor(quint32 index);
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:
......
......@@ -20,6 +20,8 @@
#include "dlg_layersplit.h"
#include <klocalizedstring.h>
#include <KoResourceServerProvider.h>
#include <kis_debug.h>
#include <KisViewManager.h>
......@@ -45,6 +47,11 @@ DlgLayerSplit::DlgLayerSplit()
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;
m_page->intFuzziness->setValue(cfg.readEntry<int>("layersplit/fuzziness", 20));
m_page->chkCreateGroupLayer->setChecked(cfg.readEntry<bool>("layerspit/createmastergroup", true));
......@@ -54,6 +61,17 @@ DlgLayerSplit::DlgLayerSplit()
m_page->chkSortLayers->setChecked(cfg.readEntry<bool>("layerspit/sortlayers", 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()));
setMainWidget(m_page);
......@@ -73,6 +91,7 @@ void DlgLayerSplit::applyClicked()
cfg.writeEntry("layerspit/hideoriginal", m_page->chkHideOriginal->isChecked());
cfg.writeEntry("layerspit/sortlayers", m_page->chkSortLayers->isChecked());
cfg.writeEntry("layerspit/disregardopacity", m_page->chkDisregardOpacity->isChecked());
cfg.writeEntry("layersplit/paletteName", m_palette->name());
accept();
}
......@@ -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 @@
#define DLG_LAYERSPLIT
#include <KoDialog.h>
#include <KoColorSet.h>
#include <KisColorsetChooser.h>
#include <kis_types.h>
#include "wdg_layersplit.h"
......@@ -46,13 +47,17 @@ public:
bool sortLayers() const;
bool disregardOpacity() const;
int fuzziness() const;
KoColorSet* palette() const;
private Q_SLOTS:
void applyClicked();
void slotSetPalette(KoColorSet *pal);
private:
WdgLayerSplit* m_page;
KisColorsetChooser *m_colorSetChooser;
KoColorSet *m_palette;
};
#endif // DLG_LAYERSPLIT
......@@ -149,9 +149,13 @@ void LayerSplit::slotLayerSplit()
}
if (!found) {
QString name = dlg.palette()->closestColorName(c);
if (name.toLower() == "untitled" || name.toLower() == "none" || name.toLower() == "") {
name = KoColor::toQString(c);
}
Layer l;
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->moveTo(col, row);
memcpy(l.accessor->rawData(), acc->rawDataConst(), cs->pixelSize());
......
......@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>449</width>
<height>271</height>
<height>273</height>
</rect>
</property>
<property name="windowTitle">
......@@ -77,7 +77,7 @@
</widget>
</item>
<item row="6" column="1">
<widget class="KisSliderSpinBox" name="intFuzziness"/>
<widget class="KisSliderSpinBox" name="intFuzziness" native="true"/>
</item>
<item row="5" column="0">
<widget class="QCheckBox" name="chkDisregardOpacity">
......@@ -86,6 +86,20 @@
</property>
</widget>
</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>
</item>
<item>
......@@ -123,6 +137,11 @@
</layout>
</widget>
<customwidgets>
<customwidget>
<class>KisPopupButton</class>
<extends>QPushButton</extends>
<header>kis_popup_button.h</header>
</customwidget>
<customwidget>
<class>KisSliderSpinBox</class>
<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