2021-08-22 20:23:18 +08:00
|
|
|
// Copyright (c) 2014-2021 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
|
2021-08-22 20:23:18 +08:00
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
2015-12-25 04:51:11 +08:00
|
|
|
// 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 09:20:12 +08:00
|
|
|
#include <cmath> // for std::fabs
|
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
#include <xlnt/styles/fill.hpp>
|
|
|
|
|
|
|
|
namespace xlnt {
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
// pattern_fill
|
2015-12-25 04:51:11 +08:00
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
pattern_fill::pattern_fill()
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
pattern_fill_type pattern_fill::type() const
|
2016-06-13 07:59:59 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return type_;
|
2016-06-13 07:59:59 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
pattern_fill &pattern_fill::type(pattern_fill_type type)
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
type_ = type;
|
|
|
|
return *this;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
optional<color> pattern_fill::foreground() const
|
2016-03-14 11:46:01 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return foreground_;
|
2016-03-14 11:46:01 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
pattern_fill &pattern_fill::foreground(const color &new_foreground)
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
foreground_ = new_foreground;
|
2016-10-30 00:41:55 +08:00
|
|
|
|
2017-01-27 08:57:19 +08:00
|
|
|
if (!background_.is_set())
|
2016-12-24 23:04:57 +08:00
|
|
|
{
|
|
|
|
background_.set(indexed_color(64));
|
|
|
|
}
|
2016-10-30 00:41:55 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return *this;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
optional<color> pattern_fill::background() const
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return background_;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
pattern_fill &pattern_fill::background(const color &new_background)
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
background_ = new_background;
|
|
|
|
return *this;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
bool pattern_fill::operator==(const pattern_fill &other) const
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2017-03-25 23:22:21 +08:00
|
|
|
if (background().is_set() != other.background().is_set())
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
if (background().is_set())
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-03-25 23:22:21 +08:00
|
|
|
if (background().get() != other.background().get())
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 12:23:49 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
if (foreground().is_set() != other.foreground().is_set())
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
if (foreground().is_set())
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-03-25 23:22:21 +08:00
|
|
|
if (foreground().get() != other.foreground().get())
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
if (type() != other.type())
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-01 08:48:43 +08:00
|
|
|
return true;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
bool pattern_fill::operator!=(const pattern_fill &other) const
|
|
|
|
{
|
2019-12-27 01:03:12 +08:00
|
|
|
return !(*this == other);
|
2017-03-25 23:22:21 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
// gradient_fill
|
|
|
|
|
2019-12-27 01:03:12 +08:00
|
|
|
gradient_fill::gradient_fill()
|
|
|
|
: type_(gradient_fill_type::linear)
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
gradient_fill_type gradient_fill::type() const
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return type_;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
gradient_fill &gradient_fill::type(gradient_fill_type t)
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
type_ = t;
|
|
|
|
return *this;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
gradient_fill &gradient_fill::degree(double degree)
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
degree_ = degree;
|
|
|
|
return *this;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
double gradient_fill::degree() const
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return degree_;
|
2016-08-16 12:23:49 +08:00
|
|
|
}
|
2016-07-15 10:09:57 +08:00
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
double gradient_fill::left() const
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return left_;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
gradient_fill &gradient_fill::left(double value)
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
left_ = value;
|
|
|
|
return *this;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
double gradient_fill::right() const
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return right_;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
gradient_fill &gradient_fill::right(double value)
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
right_ = value;
|
|
|
|
return *this;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
double gradient_fill::top() const
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return top_;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
gradient_fill &gradient_fill::top(double value)
|
2016-05-17 07:31:44 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
top_ = value;
|
|
|
|
return *this;
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
double gradient_fill::bottom() const
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return bottom_;
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
gradient_fill &gradient_fill::bottom(double value)
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
bottom_ = value;
|
|
|
|
return *this;
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
gradient_fill &gradient_fill::add_stop(double position, color stop_color)
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
stops_[position] = stop_color;
|
|
|
|
return *this;
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
gradient_fill &gradient_fill::clear_stops()
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
stops_.clear();
|
|
|
|
return *this;
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
std::unordered_map<double, color> gradient_fill::stops() const
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return stops_;
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
bool gradient_fill::operator==(const gradient_fill &other) const
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-03-25 23:22:21 +08:00
|
|
|
if (type() != other.type())
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
if (std::fabs(degree() - other.degree()) != 0.)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
if (std::fabs(bottom() - other.bottom()) != 0.)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
if (std::fabs(right() - other.right()) != 0.)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
if (std::fabs(top() - other.top()) != 0.)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
if (std::fabs(left() - other.left()) != 0.)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
if (stops() != other.stops())
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-01 08:48:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
bool gradient_fill::operator!=(const gradient_fill &other) const
|
|
|
|
{
|
2019-12-27 01:03:12 +08:00
|
|
|
return !(*this == other);
|
2017-03-25 23:22:21 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
// fill
|
2016-07-15 10:09:57 +08:00
|
|
|
|
2016-10-30 00:17:22 +08:00
|
|
|
fill fill::solid(const color &fill_color)
|
|
|
|
{
|
2017-04-04 07:24:36 +08:00
|
|
|
return fill(xlnt::pattern_fill()
|
2019-12-27 01:03:12 +08:00
|
|
|
.type(xlnt::pattern_fill_type::solid)
|
|
|
|
.foreground(fill_color)
|
|
|
|
.background(indexed_color(64)));
|
2016-10-30 00:17:22 +08:00
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
fill::fill()
|
|
|
|
: type_(fill_type::pattern)
|
2016-08-16 12:23:49 +08:00
|
|
|
{
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
fill::fill(const xlnt::pattern_fill &pattern)
|
|
|
|
: type_(fill_type::pattern), pattern_(pattern)
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
fill::fill(const xlnt::gradient_fill &gradient)
|
|
|
|
: type_(fill_type::gradient), gradient_(gradient)
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
fill_type fill::type() const
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2016-08-16 12:23:49 +08:00
|
|
|
return type_;
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
gradient_fill fill::gradient_fill() const
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2016-08-16 12:23:49 +08:00
|
|
|
if (type_ != fill_type::gradient)
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
throw invalid_attribute();
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return gradient_;
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
pattern_fill fill::pattern_fill() const
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2016-08-16 12:23:49 +08:00
|
|
|
if (type_ != fill_type::pattern)
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
throw invalid_attribute();
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return pattern_;
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
bool fill::operator==(const fill &other) const
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2017-03-25 23:22:21 +08:00
|
|
|
if (type() != other.type())
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
if (type() == fill_type::gradient)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-03-25 23:22:21 +08:00
|
|
|
return gradient_fill() == other.gradient_fill();
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
2016-06-20 04:30:15 +08:00
|
|
|
|
2017-03-25 23:22:21 +08:00
|
|
|
return pattern_fill() == other.pattern_fill();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fill::operator!=(const fill &other) const
|
|
|
|
{
|
2019-12-27 01:03:12 +08:00
|
|
|
return !(*this == other);
|
2016-06-20 04:30:15 +08:00
|
|
|
}
|
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
} // namespace xlnt
|