2017-01-03 09:35:18 +08:00
|
|
|
// Copyright (c) 2014-2017 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 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
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
if (!background_)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-01 09:53:22 +08:00
|
|
|
XLNT_API bool operator==(const pattern_fill &left, const pattern_fill &right)
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-11-01 08:48:43 +08:00
|
|
|
if (left.background().is_set() != right.background().is_set())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-01 08:48:43 +08:00
|
|
|
if (left.background().is_set())
|
|
|
|
{
|
|
|
|
if (left.background().get() != right.background().get())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 12:23:49 +08:00
|
|
|
|
2016-11-01 08:48:43 +08:00
|
|
|
if (left.foreground().is_set() != right.foreground().is_set())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-01 08:48:43 +08:00
|
|
|
if (left.foreground().is_set())
|
|
|
|
{
|
|
|
|
if (left.foreground().get() != right.foreground().get())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-01 08:48:43 +08:00
|
|
|
if (left.type() != right.type())
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
// gradient_fill
|
|
|
|
|
2016-12-24 23:04:57 +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
|
|
|
}
|
|
|
|
|
2016-11-01 09:53:22 +08:00
|
|
|
XLNT_API bool operator==(const gradient_fill &left, const gradient_fill &right)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
if (left.type() != right.type())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-10 08:52:18 +08:00
|
|
|
if (std::fabs(left.degree() - right.degree()) != 0.)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-10 08:52:18 +08:00
|
|
|
if (std::fabs(left.bottom() - right.bottom()) != 0.)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-10 08:52:18 +08:00
|
|
|
if (std::fabs(left.right() - right.right()) != 0.)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-10 08:52:18 +08:00
|
|
|
if (std::fabs(left.top() - right.top()) != 0.)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-10 08:52:18 +08:00
|
|
|
if (std::fabs(left.left() - right.left()) != 0.)
|
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
|
|
|
if (left.stops() != right.stops())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-01 08:48:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return fill(
|
|
|
|
xlnt::pattern_fill().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_;
|
|
|
|
}
|
|
|
|
|
2016-11-01 09:53:22 +08:00
|
|
|
XLNT_API bool operator==(const fill &left, const fill &right)
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2016-11-01 08:48:43 +08:00
|
|
|
if (left.type() != right.type())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-01 08:48:43 +08:00
|
|
|
if (left.type() == fill_type::gradient)
|
|
|
|
{
|
|
|
|
return left.gradient_fill() == right.gradient_fill();
|
|
|
|
}
|
2016-06-20 04:30:15 +08:00
|
|
|
|
2016-11-01 08:48:43 +08:00
|
|
|
return left.pattern_fill() == right.pattern_fill();
|
2016-06-20 04:30:15 +08:00
|
|
|
}
|
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
} // namespace xlnt
|