xlnt/source/styles/color.cpp

244 lines
5.0 KiB
C++
Raw Normal View History

2015-12-25 06:10:02 +08:00
// Copyright (c) 2014-2016 Thomas Fussell
// 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-08-06 22:50:29 +08:00
#include <cstdlib>
#include <xlnt/styles/color.hpp>
2016-08-02 06:33:43 +08:00
#include <xlnt/utils/exceptions.hpp>
namespace xlnt {
2016-08-02 06:33:43 +08:00
indexed_color::indexed_color(std::size_t index) : index_(index)
{
}
theme_color::theme_color(std::size_t index) : index_(index)
{
}
const color color::black()
{
2016-08-02 06:33:43 +08:00
return color(rgb_color("ff000000"));
}
const color color::white()
{
2016-08-02 06:33:43 +08:00
return color(rgb_color("ffffffff"));
}
const color color::red()
{
2016-08-02 06:33:43 +08:00
return color(rgb_color("ffff0000"));
}
const color color::darkred()
{
2016-08-02 06:33:43 +08:00
return color(rgb_color("ff8b0000"));
}
const color color::blue()
{
2016-08-02 06:33:43 +08:00
return color(rgb_color("ff0000ff"));
}
const color color::darkblue()
{
2016-08-02 06:33:43 +08:00
return color(rgb_color("ff00008b"));
}
const color color::green()
{
2016-08-02 06:33:43 +08:00
return color(rgb_color("ff00ff00"));
}
const color color::darkgreen()
{
2016-08-02 06:33:43 +08:00
return color(rgb_color("ff008b00"));
}
const color color::yellow()
{
2016-08-02 06:33:43 +08:00
return color(rgb_color("ffffff00"));
}
const color color::darkyellow()
{
2016-08-02 06:33:43 +08:00
return color(rgb_color("ffcccc00"));
}
color::color()
2016-08-02 06:33:43 +08:00
: type_(type::auto_),
rgb_(rgb_color(0, 0, 0, 0)),
indexed_(0),
theme_(0),
tint_(0)
{
}
2016-08-02 06:33:43 +08:00
color::color(const rgb_color &rgb)
: type_(type::rgb),
rgb_(rgb),
indexed_(0),
theme_(0),
tint_(0)
{
}
2016-08-02 06:33:43 +08:00
color::color(const indexed_color &indexed)
: type_(type::indexed),
rgb_(rgb_color(0, 0, 0, 0)),
indexed_(indexed),
theme_(0),
tint_(0)
{
}
2016-08-02 06:33:43 +08:00
color::color(const theme_color &theme)
: type_(type::theme),
rgb_(rgb_color(0, 0, 0, 0)),
indexed_(0),
theme_(theme),
tint_(0)
{
}
2016-08-02 06:33:43 +08:00
color::type color::get_type() const
{
2016-08-02 06:33:43 +08:00
return type_;
}
2016-08-02 06:33:43 +08:00
bool color::is_auto() const
{
2016-08-02 06:33:43 +08:00
return type_ == type::auto_;
}
2016-08-02 06:33:43 +08:00
const indexed_color &color::get_indexed() const
{
2016-08-02 06:33:43 +08:00
assert_type(type::indexed);
return indexed_;
}
2016-08-02 06:33:43 +08:00
const theme_color &color::get_theme() const
{
2016-08-02 06:33:43 +08:00
assert_type(type::theme);
return theme_;
}
2016-08-02 06:33:43 +08:00
std::string rgb_color::get_hex_string() const
{
static const char* digits = "0123456789abcdef";
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-08-02 06:33:43 +08:00
std::array<std::uint8_t, 4> rgb_color::decode_hex_string(const std::string &hex_string)
{
2016-08-06 22:50:29 +08:00
auto x = std::strtoul(hex_string.c_str(), NULL, 16);
2016-08-02 06:33:43 +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);
return { {r, g, b, a} };
}
2016-08-02 06:33:43 +08:00
rgb_color::rgb_color(const std::string &hex_string)
: rgba_(decode_hex_string(hex_string))
{
2016-08-02 06:33:43 +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-08-02 06:33:43 +08:00
{
}
std::size_t indexed_color::get_index() const
{
return index_;
}
2016-08-02 06:33:43 +08:00
std::size_t theme_color::get_index() const
{
return index_;
}
2016-08-02 06:33:43 +08:00
const rgb_color &color::get_rgb() const
{
2016-08-02 06:33:43 +08:00
assert_type(type::rgb);
return rgb_;
}
2016-08-02 06:33:43 +08:00
void color::set_tint(double tint)
{
tint_ = tint;
}
std::string color::to_hash_string() const
{
std::string hash_string = "color";
hash_string.append(std::to_string(static_cast<std::size_t>(type_)));
2016-08-02 06:33:43 +08:00
hash_string.append(std::to_string(static_cast<double>(tint_)));
if (type_ == type::indexed)
{
hash_string.append("indexed");
hash_string.append(std::to_string(indexed_.get_index()));
}
else if (type_ == type::theme)
{
hash_string.append("theme");
hash_string.append(std::to_string(theme_.get_index()));
}
else if (type_ == type::rgb)
{
hash_string.append("rgb");
hash_string.append(rgb_.get_hex_string());
}
return hash_string;
}
2016-08-02 06:33:43 +08:00
void color::assert_type(type t) const
{
if (t != type_)
{
throw invalid_attribute();
}
}
} // namespace xlnt