File indexing completed on 2024-04-28 11:39:08

0001 /*
0002  Copyright (C) 2007 Eric Seidel <eric@webkit.org>
0003 
0004  This file is part of the WebKit project
0005 
0006  This library is free software; you can redistribute it and/or
0007  modify it under the terms of the GNU Library General Public
0008  License as published by the Free Software Foundation; either
0009  version 2 of the License, or (at your option) any later version.
0010 
0011  This library is distributed in the hope that it will be useful,
0012  but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  Library General Public License for more details.
0015 
0016  You should have received a copy of the GNU Library General Public License
0017  along with this library; see the file COPYING.LIB.  If not, write to
0018  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  Boston, MA 02110-1301, USA.
0020  */
0021 
0022 #if ENABLE(SVG)
0023 #include "ColorDistance.h"
0024 #include "Color.h"
0025 #include <wtf/MathExtras.h>
0026 
0027 namespace WebCore
0028 {
0029 
0030 ColorDistance::ColorDistance()
0031     : m_redDiff(0)
0032     , m_greenDiff(0)
0033     , m_blueDiff(0)
0034 {
0035 }
0036 
0037 ColorDistance::ColorDistance(const Color &fromColor, const Color &toColor)
0038     : m_redDiff(toColor.red() - fromColor.red())
0039     , m_greenDiff(toColor.green() - fromColor.green())
0040     , m_blueDiff(toColor.blue() - fromColor.blue())
0041 {
0042 }
0043 
0044 ColorDistance::ColorDistance(int redDiff, int greenDiff, int blueDiff)
0045     : m_redDiff(redDiff)
0046     , m_greenDiff(greenDiff)
0047     , m_blueDiff(blueDiff)
0048 {
0049 }
0050 
0051 static inline int clampColorValue(int v)
0052 {
0053     if (v > 255) {
0054         v = 255;
0055     } else if (v < 0) {
0056         v = 0;
0057     }
0058     return v;
0059 }
0060 
0061 ColorDistance ColorDistance::scaledDistance(float scaleFactor) const
0062 {
0063     return ColorDistance(static_cast<int>(scaleFactor * m_redDiff),
0064                          static_cast<int>(scaleFactor * m_greenDiff),
0065                          static_cast<int>(scaleFactor * m_blueDiff));
0066 }
0067 
0068 Color ColorDistance::addColorsAndClamp(const Color &first, const Color &second)
0069 {
0070     return Color(clampColorValue(first.red() + second.red()),
0071                  clampColorValue(first.green() + second.green()),
0072                  clampColorValue(first.blue() + second.blue()));
0073 }
0074 
0075 Color ColorDistance::addToColorAndClamp(const Color &color) const
0076 {
0077     return Color(clampColorValue(color.red() + m_redDiff),
0078                  clampColorValue(color.green() + m_greenDiff),
0079                  clampColorValue(color.blue() + m_blueDiff));
0080 }
0081 
0082 bool ColorDistance::isZero() const
0083 {
0084     return (m_redDiff == 0 && m_blueDiff == 0 && m_greenDiff == 0);
0085 }
0086 
0087 float ColorDistance::distance() const
0088 {
0089     // This is just a simple distance calculation, not respecting color spaces
0090     return sqrtf(m_redDiff * m_redDiff + m_blueDiff * m_blueDiff + m_greenDiff * m_greenDiff);
0091 }
0092 
0093 }
0094 
0095 #endif