2002-03-11 17:00:41 +00:00
|
|
|
/**
|
|
|
|
* \file Color.C
|
2002-09-05 15:14:23 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2000-11-10 17:29:47 +00:00
|
|
|
*
|
2002-12-01 22:59:25 +00:00
|
|
|
* \author Angus Leeming
|
2002-09-05 14:10:50 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-03-11 17:00:41 +00:00
|
|
|
*/
|
2000-11-10 17:29:47 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
2001-03-15 13:37:04 +00:00
|
|
|
|
2000-11-10 17:29:47 +00:00
|
|
|
#include "Color.h"
|
2003-09-05 13:15:43 +00:00
|
|
|
|
2003-09-15 15:20:22 +00:00
|
|
|
#include "LColor.h"
|
|
|
|
|
2003-09-05 22:17:02 +00:00
|
|
|
#include <cmath>
|
2004-07-24 10:55:30 +00:00
|
|
|
#include <sstream>
|
2003-09-05 13:15:43 +00:00
|
|
|
#include <iomanip>
|
2003-08-06 18:42:58 +00:00
|
|
|
|
2002-06-10 07:57:39 +00:00
|
|
|
#ifndef CXX_GLOBAL_CSTD
|
|
|
|
using std::floor;
|
|
|
|
#endif
|
|
|
|
|
2000-11-10 17:29:47 +00:00
|
|
|
using std::max;
|
|
|
|
using std::min;
|
2003-08-06 21:54:45 +00:00
|
|
|
using std::setw;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
2003-09-05 18:02:24 +00:00
|
|
|
using std::istringstream;
|
|
|
|
using std::ostringstream;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2000-11-10 17:29:47 +00:00
|
|
|
|
2004-05-19 15:11:37 +00:00
|
|
|
namespace lyx {
|
2003-08-06 18:42:58 +00:00
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
int const nohue = -1;
|
|
|
|
|
2003-08-06 18:42:58 +00:00
|
|
|
int hexstrToInt(string const & str)
|
|
|
|
{
|
2006-04-05 23:56:29 +00:00
|
|
|
int val = 0;
|
|
|
|
istringstream is(str);
|
|
|
|
is >> std::setbase(16) >> val;
|
|
|
|
return val;
|
2003-08-06 18:42:58 +00:00
|
|
|
}
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
} // namespace anon
|
2000-11-10 17:29:47 +00:00
|
|
|
|
2003-02-11 17:53:38 +00:00
|
|
|
|
2003-08-06 18:42:58 +00:00
|
|
|
|
|
|
|
string const X11hexname(RGBColor const & col)
|
|
|
|
{
|
|
|
|
ostringstream ostr;
|
|
|
|
|
|
|
|
ostr << '#' << std::setbase(16) << std::setfill('0')
|
|
|
|
<< setw(2) << col.r
|
|
|
|
<< setw(2) << col.g
|
|
|
|
<< setw(2) << col.b;
|
|
|
|
|
2003-09-15 11:00:00 +00:00
|
|
|
return ostr.str();
|
2003-08-06 18:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RGBColor::RGBColor(string const & x11hexname)
|
2003-09-09 17:25:35 +00:00
|
|
|
: r(0), g(0), b(0)
|
2003-08-06 18:42:58 +00:00
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
BOOST_ASSERT(x11hexname.size() == 7 && x11hexname[0] == '#');
|
2003-08-06 18:42:58 +00:00
|
|
|
r = hexstrToInt(x11hexname.substr(1,2));
|
|
|
|
g = hexstrToInt(x11hexname.substr(3,2));
|
|
|
|
b = hexstrToInt(x11hexname.substr(5,2));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-14 02:01:57 +00:00
|
|
|
RGBColor::RGBColor(HSVColor const & hsv)
|
2000-11-10 17:29:47 +00:00
|
|
|
{
|
|
|
|
double h = hsv.h;
|
2000-11-14 02:01:57 +00:00
|
|
|
double const s = hsv.s;
|
|
|
|
double const v = hsv.v;
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2000-11-10 17:29:47 +00:00
|
|
|
double rd, gd, bd;
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2000-11-14 02:01:57 +00:00
|
|
|
if (h == nohue || s == 0.0) {
|
2000-11-10 17:29:47 +00:00
|
|
|
rd = gd = bd = v;
|
|
|
|
} else {
|
2000-11-14 02:01:57 +00:00
|
|
|
if (h == 360.0) h = 0.0;
|
2000-11-10 17:29:47 +00:00
|
|
|
h /= 60.0;
|
|
|
|
|
2000-11-14 02:01:57 +00:00
|
|
|
int const j = max(0, static_cast<int>(::floor(h)));
|
2002-02-16 15:59:55 +00:00
|
|
|
//if (j < 0) j = 0;
|
2000-11-10 17:29:47 +00:00
|
|
|
|
2000-11-14 02:01:57 +00:00
|
|
|
double const f = h - j;
|
|
|
|
double const p = v * (1.0 - s);
|
|
|
|
double const q = v * (1.0 - (s * f));
|
|
|
|
double const t = v * (1.0 - (s * (1.0 - f)));
|
2000-11-10 17:29:47 +00:00
|
|
|
|
2000-11-14 02:01:57 +00:00
|
|
|
switch (j) {
|
2000-11-10 17:29:47 +00:00
|
|
|
case 0:
|
|
|
|
rd = v;
|
|
|
|
gd = t;
|
|
|
|
bd = p;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
rd = q;
|
|
|
|
gd = v;
|
|
|
|
bd = p;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
rd = p;
|
|
|
|
gd = v;
|
|
|
|
bd = t;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
rd = p;
|
|
|
|
gd = q;
|
|
|
|
bd = v;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
rd = t;
|
|
|
|
gd = p;
|
|
|
|
bd = v;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
rd = v;
|
|
|
|
gd = p;
|
|
|
|
bd = q;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rd = v;
|
|
|
|
gd = t;
|
|
|
|
bd = p;
|
|
|
|
break; // should never happen.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
r = static_cast<int>(::floor((rd * 255.0) + 0.5));
|
|
|
|
g = static_cast<int>(::floor((gd * 255.0) + 0.5));
|
|
|
|
b = static_cast<int>(::floor((bd * 255.0) + 0.5));
|
2000-11-10 17:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-14 02:01:57 +00:00
|
|
|
HSVColor::HSVColor(RGBColor const & rgb)
|
2000-11-10 17:29:47 +00:00
|
|
|
{
|
2000-11-14 02:01:57 +00:00
|
|
|
double const r = rgb.r / 255.0;
|
|
|
|
double const g = rgb.g / 255.0;
|
|
|
|
double const b = rgb.b / 255.0;
|
2000-11-10 17:29:47 +00:00
|
|
|
|
2002-08-14 18:31:04 +00:00
|
|
|
double const maxval = max(max(r, g), b);
|
|
|
|
double const minval = min(min(r, g), b);
|
2000-11-10 17:29:47 +00:00
|
|
|
|
|
|
|
v = maxval;
|
|
|
|
|
2000-11-14 02:01:57 +00:00
|
|
|
double const diff = maxval - minval;
|
|
|
|
if (maxval != 0.0)
|
2000-11-10 17:29:47 +00:00
|
|
|
s = diff / maxval;
|
|
|
|
else
|
|
|
|
s = 0.0;
|
|
|
|
|
|
|
|
h = nohue;
|
2000-11-14 02:01:57 +00:00
|
|
|
if (s != 0.0) {
|
|
|
|
double const rc = (maxval - r) / diff;
|
|
|
|
double const gc = (maxval - g) / diff;
|
|
|
|
double const bc = (maxval - b) / diff;
|
2000-11-10 17:29:47 +00:00
|
|
|
|
2000-11-14 02:01:57 +00:00
|
|
|
if (r == maxval)
|
2000-11-10 17:29:47 +00:00
|
|
|
h = bc - gc;
|
2000-11-14 02:01:57 +00:00
|
|
|
else if (g == maxval)
|
2000-11-10 17:29:47 +00:00
|
|
|
h = 2.0 + rc - bc;
|
2000-11-14 02:01:57 +00:00
|
|
|
else if (b == maxval)
|
2000-11-10 17:29:47 +00:00
|
|
|
h = 4.0 + gc - rc;
|
|
|
|
|
|
|
|
h *= 60.0;
|
2000-11-14 02:01:57 +00:00
|
|
|
if (h < 0)
|
2000-11-10 17:29:47 +00:00
|
|
|
h += 360;
|
|
|
|
}
|
|
|
|
}
|
2004-05-19 15:11:37 +00:00
|
|
|
|
|
|
|
} // namespace lyx
|