File indexing completed on 2024-05-19 04:23:09

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 
0029 #define DEBUG_KP_COLOR_SIMILARITY_CUBE 0
0030 
0031 
0032 #include "kpColorSimilarityHolder.h"
0033 
0034 #include "kpColorSimilarityCubeRenderer.h"
0035 #include "imagelib/kpColor.h"
0036 #include "kpDefs.h"
0037 
0038 #include <cmath>
0039 
0040 
0041 #include "kpLogCategories.h"
0042 
0043 #include <KLocalizedString>
0044 
0045 // public static
0046 const double kpColorSimilarityHolder::ColorCubeDiagonalDistance =
0047     std::sqrt (255.0 * 255 * 3);
0048 
0049 // public static
0050 const double kpColorSimilarityHolder::MaxColorSimilarity = 0.30;
0051 
0052 
0053 kpColorSimilarityHolder::kpColorSimilarityHolder ()
0054     : m_colorSimilarity (0)
0055 {
0056 }
0057 
0058 kpColorSimilarityHolder::~kpColorSimilarityHolder () = default;
0059 
0060 
0061 // Don't cause the translators grief by appending strings etc.
0062 // - duplicate text with 2 cases
0063 
0064 // public static
0065 QString kpColorSimilarityHolder::WhatsThisWithClickInstructions ()
0066 {
0067     return i18n ("<qt>"
0068         "<p><b>Color Similarity</b> is how <i>similar</i> the colors of different pixels"
0069         " must be, for operations to consider them to be the same.</p>"
0070 
0071         "<p>If you set it to something other than <b>Exact Match</b>,"
0072         " you can work more effectively with dithered"
0073         " images and photos, in a comparable manner to the \"Magic Wand\""
0074         " feature of other paint programs.</p>"
0075 
0076         "<p>This feature applies to:</p>"
0077 
0078         "<ul>"
0079          
0080         "<li><b>Selections</b>: In <b>Transparent</b> mode, any color in the"
0081         " selection that is <i>similar</i> to the background color will"
0082         " be made transparent.</li>"
0083 
0084         "<li><b>Flood Fill</b>: For regions with <i>similar</i> - but not"
0085         " identical - colored pixels, a higher setting is likely to"
0086         " fill more pixels.</li>"
0087         
0088         "<li><b>Color Eraser</b>: Any pixel whose color is <i>similar</i>"
0089         " to the foreground color will be replaced with the background"
0090         " color.</li>"
0091 
0092         "<li><b>Autocrop</b> and <b>Remove Internal Border</b>: For"
0093         " borders with <i>similar</i> - but not identical - colored pixels,"
0094         " a higher setting is more likely to crop the whole border.</li>"
0095 
0096         "</ul>"
0097         
0098         "<p>Higher settings mean that operations consider an increased range"
0099         " of colors to be sufficiently <i>similar</i> so as to be the same. Therefore,"
0100         " you should increase the setting if the above operations are not"
0101         " affecting pixels whose colors you consider to be similar enough.</p>"
0102 
0103         "<p>However, if they are having too much of an effect and are changing"
0104         " pixels whose colors you do not consider to be similar"
0105         " (e.g. if <b>Flood Fill</b> is changing too many pixels), you"
0106         " should decrease this setting.</p>"
0107 
0108         // sync: Compared to the other string below, we've added this line.
0109         "<p>To configure it, click on the cube.</p>"
0110 
0111         "</qt>");
0112 }
0113 
0114 // public static
0115 QString kpColorSimilarityHolder::WhatsThis ()
0116 {
0117     return i18n ("<qt>"
0118         "<p><b>Color Similarity</b> is how <i>similar</i> the colors of different pixels"
0119         " must be, for operations to consider them to be the same.</p>"
0120 
0121         "<p>If you set it to something other than <b>Exact Match</b>,"
0122         " you can work more effectively with dithered"
0123         " images and photos, in a comparable manner to the \"Magic Wand\""
0124         " feature of other paint programs.</p>"
0125 
0126         "<p>This feature applies to:</p>"
0127 
0128         "<ul>"
0129          
0130         "<li><b>Selections</b>: In <b>Transparent</b> mode, any color in the"
0131         " selection that is <i>similar</i> to the background color will"
0132         " be made transparent.</li>"
0133 
0134         "<li><b>Flood Fill</b>: For regions with <i>similar</i> - but not"
0135         " identical - colored pixels, a higher setting is likely to"
0136         " fill more pixels.</li>"
0137         
0138         "<li><b>Color Eraser</b>: Any pixel whose color is <i>similar</i>"
0139         " to the foreground color will be replaced with the background"
0140         " color.</li>"
0141 
0142         "<li><b>Autocrop</b> and <b>Remove Internal Border</b>: For"
0143         " borders with <i>similar</i> - but not identical - colored pixels,"
0144         " a higher setting is more likely to crop the whole border.</li>"
0145 
0146         "</ul>"
0147         
0148         "<p>Higher settings mean that operations consider an increased range"
0149         " of colors to be sufficiently <i>similar</i> so as to be the same. Therefore,"
0150         " you should increase the setting if the above operations are not"
0151         " affecting pixels whose colors you consider to be similar enough.</p>"
0152 
0153         "<p>However, if they are having too much of an effect and are changing"
0154         " pixels whose colors you do not consider to be similar"
0155         " (e.g. if <b>Flood Fill</b> is changing too many pixels), you"
0156         " should decrease this setting.</p>"
0157 
0158         "</qt>");
0159 }
0160 
0161 
0162 // public
0163 double kpColorSimilarityHolder::colorSimilarity () const
0164 {
0165     return m_colorSimilarity;
0166 }
0167 
0168 // public virtual
0169 void kpColorSimilarityHolder::setColorSimilarity (double similarity)
0170 {
0171 #if DEBUG_KP_COLOR_SIMILARITY_CUBE
0172     qCDebug(kpLogWidgets) << "kpColorSimilarityHolder::setColorSimilarity(" << similarity << ")";
0173 #endif
0174 
0175     if (m_colorSimilarity == similarity) {
0176         return;
0177     }
0178 
0179     if (similarity < 0) {
0180         similarity = 0;
0181     }
0182     else if (similarity > MaxColorSimilarity) {
0183         similarity = MaxColorSimilarity;
0184     }
0185 
0186     m_colorSimilarity = similarity;
0187 }