2020-02-09 01:12:59 +08:00
|
|
|
// Copyright (c) 2014-2020 Thomas Fussell
|
2015-12-25 04:51:11 +08:00
|
|
|
// Copyright (c) 2010-2015 openpyxl
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE
|
|
|
|
//
|
|
|
|
// @license: http://www.opensource.org/licenses/mit-license.php
|
|
|
|
// @author: see AUTHORS file
|
|
|
|
|
2016-11-10 08:52:18 +08:00
|
|
|
#include <cmath>
|
2016-08-06 22:50:29 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
#include <xlnt/styles/color.hpp>
|
2016-08-02 06:33:43 +08:00
|
|
|
#include <xlnt/utils/exceptions.hpp>
|
2015-12-25 04:51:11 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::array<std::uint8_t, 4> decode_hex_string(const std::string &hex_string)
|
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
auto x = std::strtoul(hex_string.c_str(), nullptr, 16);
|
2017-03-25 23:22:21 +08:00
|
|
|
|
2018-07-23 11:51:33 +08:00
|
|
|
auto a = static_cast<std::uint8_t>(x >> 24);
|
|
|
|
auto r = static_cast<std::uint8_t>((x >> 16) & 0xff);
|
|
|
|
auto g = static_cast<std::uint8_t>((x >> 8) & 0xff);
|
|
|
|
auto b = static_cast<std::uint8_t>(x & 0xff);
|
2017-03-25 23:22:21 +08:00
|
|
|
|
2019-12-27 01:03:12 +08:00
|
|
|
return {{r, g, b, a}};
|
2017-03-25 23:22:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
namespace xlnt {
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
// indexed_color implementation
|
|
|
|
|
2019-12-27 01:03:12 +08:00
|
|
|
indexed_color::indexed_color(std::size_t index)
|
|
|
|
: index_(index)
|
2016-08-02 06:33:43 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
std::size_t indexed_color::index() const
|
2016-08-02 06:33:43 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return index_;
|
2016-08-02 06:33:43 +08:00
|
|
|
}
|
|
|
|
|
2018-07-02 06:43:09 +08:00
|
|
|
void indexed_color::index(std::size_t index)
|
|
|
|
{
|
|
|
|
index_ = index;
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
// theme_color implementation
|
|
|
|
|
2019-12-27 01:03:12 +08:00
|
|
|
theme_color::theme_color(std::size_t index)
|
|
|
|
: index_(index)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
std::size_t theme_color::index() const
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return index_;
|
2016-05-01 04:19:45 +08:00
|
|
|
}
|
|
|
|
|
2018-07-02 06:43:09 +08:00
|
|
|
void theme_color::index(std::size_t index)
|
|
|
|
{
|
|
|
|
index_ = index;
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
// rgb_color implementation
|
|
|
|
|
|
|
|
std::string rgb_color::hex_string() const
|
2016-05-17 07:31:44 +08:00
|
|
|
{
|
2018-04-25 05:58:17 +08:00
|
|
|
static const char *digits = "0123456789ABCDEF";
|
2017-03-25 23:22:21 +08:00
|
|
|
std::string hex_string(8, '0');
|
|
|
|
auto out_iter = hex_string.begin();
|
|
|
|
|
|
|
|
for (auto byte : {rgba_[3], rgba_[0], rgba_[1], rgba_[2]})
|
|
|
|
{
|
|
|
|
for (auto i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
auto nibble = byte >> (4 * (1 - i)) & 0xf;
|
|
|
|
*(out_iter++) = digits[nibble];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex_string;
|
2016-05-17 07:31:44 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
rgb_color::rgb_color(const std::string &hex_string)
|
|
|
|
: rgba_(decode_hex_string(hex_string))
|
2016-05-17 07:31:44 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
rgb_color::rgb_color(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a)
|
|
|
|
: rgba_({{r, g, b, a}})
|
2016-05-17 07:31:44 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
std::uint8_t rgb_color::red() const
|
2016-05-17 07:31:44 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return rgba_[0];
|
2016-05-17 07:31:44 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
std::uint8_t rgb_color::green() const
|
2016-05-17 07:31:44 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return rgba_[1];
|
2016-05-17 07:31:44 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
std::uint8_t rgb_color::blue() const
|
2016-05-17 07:31:44 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return rgba_[2];
|
2016-05-17 07:31:44 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
std::uint8_t rgb_color::alpha() const
|
2016-05-17 07:31:44 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return rgba_[3];
|
2016-05-17 07:31:44 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
std::array<std::uint8_t, 3> rgb_color::rgb() const
|
2016-05-17 07:31:44 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return {{red(), green(), blue()}};
|
2016-05-17 07:31:44 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
std::array<std::uint8_t, 4> rgb_color::rgba() const
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return rgba_;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
// color implementation
|
|
|
|
|
|
|
|
const color color::black()
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return color(rgb_color("ff000000"));
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
const color color::white()
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return color(rgb_color("ffffffff"));
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
const color color::red()
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return color(rgb_color("ffff0000"));
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
const color color::darkred()
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return color(rgb_color("ff8b0000"));
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
const color color::blue()
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return color(rgb_color("ff0000ff"));
|
2016-12-02 21:37:50 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
const color color::darkblue()
|
2016-12-02 21:37:50 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return color(rgb_color("ff00008b"));
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
const color color::green()
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return color(rgb_color("ff00ff00"));
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
const color color::darkgreen()
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return color(rgb_color("ff008b00"));
|
2016-08-02 06:33:43 +08:00
|
|
|
}
|
2015-12-25 04:51:11 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
const color color::yellow()
|
2016-08-02 06:33:43 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return color(rgb_color("ffffff00"));
|
2017-03-25 23:22:21 +08:00
|
|
|
}
|
2016-08-02 06:33:43 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
const color color::darkyellow()
|
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return color(rgb_color("ffcccc00"));
|
2017-03-25 23:22:21 +08:00
|
|
|
}
|
2016-08-02 06:33:43 +08:00
|
|
|
|
2019-12-27 01:03:12 +08:00
|
|
|
color::color()
|
|
|
|
: color(indexed_color(0))
|
2017-03-25 23:22:21 +08:00
|
|
|
{
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
color::color(const rgb_color &rgb)
|
2019-12-27 01:03:12 +08:00
|
|
|
: type_(color_type::rgb),
|
|
|
|
rgb_(rgb),
|
|
|
|
indexed_(0),
|
|
|
|
theme_(0)
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2017-03-25 23:22:21 +08:00
|
|
|
}
|
2016-08-02 06:33:43 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
color::color(const indexed_color &indexed)
|
2019-12-27 01:03:12 +08:00
|
|
|
: type_(color_type::indexed),
|
|
|
|
rgb_(rgb_color(0, 0, 0, 0)),
|
|
|
|
indexed_(indexed),
|
|
|
|
theme_(0)
|
2017-03-25 23:22:21 +08:00
|
|
|
{
|
|
|
|
}
|
2015-12-25 04:51:11 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
color::color(const theme_color &theme)
|
2019-12-27 01:03:12 +08:00
|
|
|
: type_(color_type::theme),
|
|
|
|
rgb_(rgb_color(0, 0, 0, 0)),
|
|
|
|
indexed_(0),
|
|
|
|
theme_(theme)
|
2017-03-25 23:22:21 +08:00
|
|
|
{
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
color_type color::type() const
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return type_;
|
2016-08-02 06:33:43 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
bool color::auto_() const
|
2016-08-02 06:33:43 +08:00
|
|
|
{
|
2018-07-28 10:44:49 +08:00
|
|
|
return auto_color;
|
2016-08-02 06:33:43 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
void color::auto_(bool value)
|
2016-08-02 06:33:43 +08:00
|
|
|
{
|
2018-07-28 10:44:49 +08:00
|
|
|
auto_color = value;
|
2016-08-02 06:33:43 +08:00
|
|
|
}
|
2015-12-25 04:51:11 +08:00
|
|
|
|
2019-12-27 01:03:12 +08:00
|
|
|
const indexed_color &color::indexed() const
|
2017-03-25 23:22:21 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
assert_type(color_type::indexed);
|
|
|
|
return indexed_;
|
2017-03-25 23:22:21 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:19:56 +08:00
|
|
|
indexed_color &color::indexed()
|
|
|
|
{
|
|
|
|
assert_type(color_type::indexed);
|
|
|
|
return indexed_;
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:03:12 +08:00
|
|
|
const theme_color &color::theme() const
|
2016-08-02 06:33:43 +08:00
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
assert_type(color_type::theme);
|
|
|
|
return theme_;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:19:56 +08:00
|
|
|
theme_color &color::theme()
|
|
|
|
{
|
|
|
|
assert_type(color_type::theme);
|
|
|
|
return theme_;
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:03:12 +08:00
|
|
|
const rgb_color &color::rgb() const
|
2018-07-10 13:19:56 +08:00
|
|
|
{
|
|
|
|
assert_type(color_type::rgb);
|
|
|
|
return rgb_;
|
|
|
|
}
|
|
|
|
|
|
|
|
rgb_color &color::rgb()
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
assert_type(color_type::rgb);
|
|
|
|
return rgb_;
|
2016-08-02 06:33:43 +08:00
|
|
|
}
|
2015-12-25 04:51:11 +08:00
|
|
|
|
2018-07-28 10:44:49 +08:00
|
|
|
bool color::has_tint() const
|
|
|
|
{
|
|
|
|
return tint_.is_set();
|
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
void color::tint(double tint)
|
2016-08-02 06:33:43 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
tint_ = tint;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
double color::tint() const
|
|
|
|
{
|
2018-07-28 10:44:49 +08:00
|
|
|
return tint_.is_set() ? tint_.get() : 0.0;
|
2017-03-25 23:22:21 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
void color::assert_type(color_type t) const
|
2016-08-02 06:33:43 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
if (t != type_)
|
|
|
|
{
|
|
|
|
throw invalid_attribute();
|
|
|
|
}
|
2016-08-02 06:33:43 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
bool color::operator==(const xlnt::color &other) const
|
2016-10-29 22:23:04 +08:00
|
|
|
{
|
2018-07-28 10:44:49 +08:00
|
|
|
if (type_ != other.type_ || auto_color != other.auto_color)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (tint_.is_set() != other.tint_.is_set() || (tint_.is_set() && std::fabs(tint_.get() - other.tint_.get()) != 0.0))
|
2016-11-10 08:52:18 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
switch (type_)
|
2016-10-29 22:23:04 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
case color_type::indexed:
|
|
|
|
return indexed_.index() == other.indexed_.index();
|
|
|
|
case color_type::theme:
|
|
|
|
return theme_.index() == other.theme_.index();
|
|
|
|
case color_type::rgb:
|
2018-07-30 03:07:32 +08:00
|
|
|
return rgb_.rgba() == other.rgb_.rgba();
|
2016-10-29 22:23:04 +08:00
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2016-10-29 22:23:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
bool color::operator!=(const color &other) const
|
|
|
|
{
|
2018-07-23 11:51:33 +08:00
|
|
|
return !(*this == other);
|
2017-03-25 23:22:21 +08:00
|
|
|
}
|
|
|
|
|
2015-12-25 04:58:31 +08:00
|
|
|
} // namespace xlnt
|