2013-03-28 16:42:15 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
|
|
|
|
* Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors.
|
|
|
|
*
|
|
|
|
* Color class
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gal/color4d.h>
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
using namespace KIGFX;
|
2013-03-28 16:42:15 +00:00
|
|
|
|
2013-06-24 08:12:36 +00:00
|
|
|
COLOR4D::COLOR4D( EDA_COLOR_T aColor )
|
|
|
|
{
|
2013-09-11 08:30:39 +00:00
|
|
|
r = g_ColorRefs[aColor].m_Red / 255.0;
|
|
|
|
g = g_ColorRefs[aColor].m_Green / 255.0;
|
|
|
|
b = g_ColorRefs[aColor].m_Blue / 255.0;
|
2013-06-24 08:12:36 +00:00
|
|
|
a = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-24 08:32:08 +00:00
|
|
|
#ifdef WX_COMPATIBILITY
|
|
|
|
COLOR4D::COLOR4D( const wxColour& aColor )
|
|
|
|
{
|
|
|
|
r = aColor.Red();
|
|
|
|
g = aColor.Green();
|
|
|
|
b = aColor.Blue();
|
|
|
|
a = aColor.Alpha();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-03-28 16:42:15 +00:00
|
|
|
const bool COLOR4D::operator==( const COLOR4D& aColor )
|
|
|
|
{
|
|
|
|
return a == aColor.a && r == aColor.r && g == aColor.g && b == aColor.b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const bool COLOR4D::operator!=( const COLOR4D& aColor )
|
|
|
|
{
|
|
|
|
return a != aColor.a || r != aColor.r || g != aColor.g || b != aColor.b;
|
|
|
|
}
|
2013-09-12 09:35:42 +00:00
|
|
|
|
2013-09-12 15:42:28 +00:00
|
|
|
|
|
|
|
void COLOR4D::ToHSV( double& aOutH, double& aOutS, double& aOutV ) const
|
2013-09-12 09:35:42 +00:00
|
|
|
{
|
2013-09-12 15:42:28 +00:00
|
|
|
double min, max, delta;
|
2013-09-12 09:35:42 +00:00
|
|
|
|
|
|
|
min = r < g ? r : g;
|
2013-09-12 15:42:28 +00:00
|
|
|
min = min < b ? min : b;
|
2013-09-12 09:35:42 +00:00
|
|
|
|
|
|
|
max = r > g ? r : g;
|
2013-09-12 15:42:28 +00:00
|
|
|
max = max > b ? max : b;
|
2013-09-12 09:35:42 +00:00
|
|
|
|
2013-09-12 15:42:28 +00:00
|
|
|
aOutV = max; // v
|
2013-09-12 09:35:42 +00:00
|
|
|
delta = max - min;
|
2013-09-12 15:42:28 +00:00
|
|
|
|
|
|
|
if( max > 0.0 )
|
|
|
|
{
|
|
|
|
aOutS = ( delta / max ); // s
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-12 09:35:42 +00:00
|
|
|
// r = g = b = 0 // s = 0, v is undefined
|
2013-09-12 15:42:28 +00:00
|
|
|
aOutS = 0.0;
|
|
|
|
aOutH = NAN; // its now undefined
|
2013-09-12 09:35:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-09-12 15:42:28 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
if( r >= max ) // > is bogus, just keeps compiler happy
|
|
|
|
aOutH = ( g - b ) / delta; // between yellow & magenta
|
2013-09-12 15:42:28 +00:00
|
|
|
else if( g >= max )
|
2013-10-14 14:13:35 +00:00
|
|
|
aOutH = 2.0 + ( b - r ) / delta; // between cyan & yellow
|
2013-09-12 09:35:42 +00:00
|
|
|
else
|
2013-10-14 14:13:35 +00:00
|
|
|
aOutH = 4.0 + ( r - g ) / delta; // between magenta & cyan
|
2013-09-12 09:35:42 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
aOutH *= 60.0; // degrees
|
2013-09-12 09:35:42 +00:00
|
|
|
|
2013-09-12 15:42:28 +00:00
|
|
|
if( aOutH < 0.0 )
|
|
|
|
aOutH += 360.0;
|
2013-09-12 09:35:42 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 15:42:28 +00:00
|
|
|
|
|
|
|
void COLOR4D::FromHSV( double aInH, double aInS, double aInV )
|
2013-09-12 09:35:42 +00:00
|
|
|
{
|
2013-09-12 15:42:28 +00:00
|
|
|
double hh, p, q, t, ff;
|
|
|
|
long i;
|
2013-09-12 09:35:42 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
if( aInS <= 0.0 ) // < is bogus, just shuts up warnings
|
2013-09-12 15:42:28 +00:00
|
|
|
{
|
|
|
|
r = aInV;
|
|
|
|
g = aInV;
|
|
|
|
b = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-09-12 15:42:28 +00:00
|
|
|
|
|
|
|
hh = aInH;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-12 15:42:28 +00:00
|
|
|
if( hh >= 360.0 )
|
|
|
|
hh = 0.0;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
hh /= 60.0;
|
2013-09-12 15:42:28 +00:00
|
|
|
|
|
|
|
i = (long) hh;
|
2013-09-12 09:35:42 +00:00
|
|
|
ff = hh - i;
|
|
|
|
|
2013-09-12 15:42:28 +00:00
|
|
|
p = aInV * ( 1.0 - aInS );
|
|
|
|
q = aInV * ( 1.0 - ( aInS * ff ) );
|
|
|
|
t = aInV * ( 1.0 - ( aInS * ( 1.0 - ff ) ) );
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
switch( i )
|
2013-09-12 15:42:28 +00:00
|
|
|
{
|
2013-09-12 09:35:42 +00:00
|
|
|
case 0:
|
2013-09-12 15:42:28 +00:00
|
|
|
r = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
g = t;
|
|
|
|
b = p;
|
|
|
|
break;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
case 1:
|
|
|
|
r = q;
|
2013-09-12 15:42:28 +00:00
|
|
|
g = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
b = p;
|
|
|
|
break;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
case 2:
|
|
|
|
r = p;
|
2013-09-12 15:42:28 +00:00
|
|
|
g = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
b = t;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
r = p;
|
|
|
|
g = q;
|
2013-09-12 15:42:28 +00:00
|
|
|
b = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
break;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
case 4:
|
|
|
|
r = t;
|
|
|
|
g = p;
|
2013-09-12 15:42:28 +00:00
|
|
|
b = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
break;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
case 5:
|
|
|
|
default:
|
2013-09-12 15:42:28 +00:00
|
|
|
r = aInV;
|
2013-09-12 09:35:42 +00:00
|
|
|
g = p;
|
|
|
|
b = q;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 15:42:28 +00:00
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
COLOR4D& COLOR4D::Saturate( double aFactor )
|
|
|
|
{
|
|
|
|
double h, s, v;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-12 15:42:28 +00:00
|
|
|
ToHSV( h, s, v );
|
|
|
|
FromHSV( h, aFactor, 1.0 );
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-09-12 09:35:42 +00:00
|
|
|
return *this;
|
|
|
|
}
|