KHTML
ColorDistance.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright (C) 2007 Eric Seidel <eric@webkit.org> 00003 00004 This file is part of the WebKit project 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "config.h" 00023 #if ENABLE(SVG) 00024 #include "ColorDistance.h" 00025 #include "Color.h" 00026 #include <wtf/MathExtras.h> 00027 00028 namespace WebCore { 00029 00030 ColorDistance::ColorDistance() 00031 : m_redDiff(0) 00032 , m_greenDiff(0) 00033 , m_blueDiff(0) 00034 { 00035 } 00036 00037 ColorDistance::ColorDistance(const Color& fromColor, const Color& toColor) 00038 : m_redDiff(toColor.red() - fromColor.red()) 00039 , m_greenDiff(toColor.green() - fromColor.green()) 00040 , m_blueDiff(toColor.blue() - fromColor.blue()) 00041 { 00042 } 00043 00044 ColorDistance::ColorDistance(int redDiff, int greenDiff, int blueDiff) 00045 : m_redDiff(redDiff) 00046 , m_greenDiff(greenDiff) 00047 , m_blueDiff(blueDiff) 00048 { 00049 } 00050 00051 static inline int clampColorValue(int v) 00052 { 00053 if (v > 255) 00054 v = 255; 00055 else if (v < 0) 00056 v = 0; 00057 return v; 00058 } 00059 00060 ColorDistance ColorDistance::scaledDistance(float scaleFactor) const 00061 { 00062 return ColorDistance(static_cast<int>(scaleFactor * m_redDiff), 00063 static_cast<int>(scaleFactor * m_greenDiff), 00064 static_cast<int>(scaleFactor * m_blueDiff)); 00065 } 00066 00067 Color ColorDistance::addColorsAndClamp(const Color& first, const Color& second) 00068 { 00069 return Color(clampColorValue(first.red() + second.red()), 00070 clampColorValue(first.green() + second.green()), 00071 clampColorValue(first.blue() + second.blue())); 00072 } 00073 00074 Color ColorDistance::addToColorAndClamp(const Color& color) const 00075 { 00076 return Color(clampColorValue(color.red() + m_redDiff), 00077 clampColorValue(color.green() + m_greenDiff), 00078 clampColorValue(color.blue() + m_blueDiff)); 00079 } 00080 00081 bool ColorDistance::isZero() const 00082 { 00083 return (m_redDiff == 0 && m_blueDiff == 0 && m_greenDiff == 0); 00084 } 00085 00086 float ColorDistance::distance() const 00087 { 00088 // This is just a simple distance calculation, not respecting color spaces 00089 return sqrtf(m_redDiff * m_redDiff + m_blueDiff * m_blueDiff + m_greenDiff * m_greenDiff); 00090 } 00091 00092 } 00093 00094 #endif
KDE 4.6 API Reference