KHTML
ColorDistance.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00089 return sqrtf(m_redDiff * m_redDiff + m_blueDiff * m_blueDiff + m_greenDiff * m_greenDiff);
00090 }
00091
00092 }
00093
00094 #endif