File indexing completed on 2025-02-09 04:59:50

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_SELECTION_TRANSPARENCY 0
0030 
0031 
0032 #include "layers/selections/image/kpImageSelectionTransparency.h"
0033 
0034 #include "kpLogCategories.h"
0035 
0036 #include "widgets/colorSimilarity/kpColorSimilarityHolder.h"
0037 
0038 
0039 //---------------------------------------------------------------------
0040 
0041 kpImageSelectionTransparency::kpImageSelectionTransparency ()
0042     : m_isOpaque (true)
0043 {
0044     setColorSimilarity (0);
0045 }
0046 
0047 //---------------------------------------------------------------------
0048 
0049 kpImageSelectionTransparency::kpImageSelectionTransparency (const kpColor &transparentColor, double colorSimilarity)
0050     : m_isOpaque (false),
0051       m_transparentColor (transparentColor)
0052 {
0053     setColorSimilarity (colorSimilarity);
0054 }
0055 
0056 //---------------------------------------------------------------------
0057 
0058 kpImageSelectionTransparency::kpImageSelectionTransparency (bool isOpaque, const kpColor &transparentColor,
0059                                                   double colorSimilarity)
0060     : m_isOpaque (isOpaque),
0061       m_transparentColor (transparentColor)
0062 {
0063     setColorSimilarity (colorSimilarity);
0064 }
0065 
0066 //---------------------------------------------------------------------
0067 
0068 bool kpImageSelectionTransparency::operator== (const kpImageSelectionTransparency &rhs) const
0069 {
0070 #if DEBUG_KP_SELECTION_TRANSPARENCY && 0
0071     qCDebug(kpLogLayers) << "kpImageSelectionTransparency::operator==()";
0072 #endif
0073     
0074     if (m_isOpaque != rhs.m_isOpaque)
0075     {
0076     #if DEBUG_KP_SELECTION_TRANSPARENCY && 0
0077         qCDebug(kpLogLayers) << "\tdifferent opacity: lhs=" << m_isOpaque
0078                    << " rhs=" << rhs.m_isOpaque
0079                    << endl;
0080     #endif
0081         return false;
0082     }
0083 
0084     if (m_isOpaque)
0085     {
0086     #if DEBUG_KP_SELECTION_TRANSPARENCY && 0
0087         qCDebug(kpLogLayers) << "\tboth opaque - eq";
0088     #endif
0089         return true;
0090     }
0091 
0092 #if DEBUG_KP_SELECTION_TRANSPARENCY && 0
0093     qCDebug(kpLogLayers) << "\tcolours: lhs=" << (int *) m_transparentColor.toQRgb ()
0094                << " rhs=" << (int *) rhs.m_transparentColor.toQRgb ()
0095                << endl;
0096     qCDebug(kpLogLayers) << "\tcolour similarity: lhs=" << m_colorSimilarity
0097                << " rhs=" << rhs.m_colorSimilarity
0098                << endl;
0099 #endif
0100     
0101     return (m_transparentColor == rhs.m_transparentColor &&
0102             m_colorSimilarity == rhs.m_colorSimilarity);
0103 }
0104 
0105 //---------------------------------------------------------------------
0106 
0107 bool kpImageSelectionTransparency::operator!= (const kpImageSelectionTransparency &rhs) const
0108 {
0109     return !(*this == rhs);
0110 }
0111 
0112 //---------------------------------------------------------------------
0113 
0114 kpImageSelectionTransparency::~kpImageSelectionTransparency () = default;
0115 
0116 //---------------------------------------------------------------------
0117 
0118 // public
0119 bool kpImageSelectionTransparency::isOpaque () const
0120 {
0121     return m_isOpaque;
0122 }
0123 
0124 //---------------------------------------------------------------------
0125 
0126 // public
0127 bool kpImageSelectionTransparency::isTransparent () const
0128 {
0129     return !isOpaque ();
0130 }
0131 
0132 //---------------------------------------------------------------------
0133 
0134 // public
0135 void kpImageSelectionTransparency::setOpaque (bool yes)
0136 {
0137     m_isOpaque = yes;
0138 }
0139 
0140 //---------------------------------------------------------------------
0141 
0142 // public
0143 void kpImageSelectionTransparency::setTransparent (bool yes)
0144 {
0145     setOpaque (!yes);
0146 }
0147 
0148 //---------------------------------------------------------------------
0149 
0150 
0151 // public
0152 kpColor kpImageSelectionTransparency::transparentColor () const
0153 {
0154     if (m_isOpaque)
0155     {
0156         // There are legitimate uses for this so no qCCritical(kpLogLayers)
0157         qCDebug(kpLogLayers) << "kpImageSelectionTransparency::transparentColor() "
0158                       "getting transparent color even though opaque";
0159     }
0160 
0161     return m_transparentColor;
0162 }
0163 
0164 //---------------------------------------------------------------------
0165 
0166 // public
0167 void kpImageSelectionTransparency::setTransparentColor (const kpColor &transparentColor)
0168 {
0169     m_transparentColor = transparentColor;
0170 }
0171 
0172 //---------------------------------------------------------------------
0173 
0174 
0175 // public
0176 double kpImageSelectionTransparency::colorSimilarity () const
0177 {
0178     if (m_colorSimilarity < 0 ||
0179         m_colorSimilarity > kpColorSimilarityHolder::MaxColorSimilarity)
0180     {
0181         qCCritical(kpLogLayers) << "kpImageSelectionTransparency::colorSimilarity() invalid colorSimilarity";
0182         return 0;
0183     }
0184 
0185     return m_colorSimilarity;
0186 }
0187 
0188 //---------------------------------------------------------------------
0189 
0190 // pubulic
0191 void kpImageSelectionTransparency::setColorSimilarity (double colorSimilarity)
0192 {
0193     m_colorSimilarity = colorSimilarity;
0194     m_processedColorSimilarity = kpColor::processSimilarity (colorSimilarity);
0195 }
0196 
0197 //---------------------------------------------------------------------
0198 
0199 // public
0200 int kpImageSelectionTransparency::processedColorSimilarity () const
0201 {
0202     return m_processedColorSimilarity;
0203 }
0204 
0205 //---------------------------------------------------------------------