2015-12-25 06:10:02 +08:00
|
|
|
// Copyright (c) 2014-2016 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
|
|
|
|
|
|
|
|
#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-08-16 12:23:49 +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-08-16 12:23:49 +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-08-16 12:23:49 +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-08-16 12:23:49 +08:00
|
|
|
foreground_ = new_foreground;
|
|
|
|
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-08-16 12:23:49 +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-08-16 12:23:49 +08:00
|
|
|
background_ = new_background;
|
|
|
|
return *this;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
std::string pattern_fill::to_hash_string() const
|
2015-12-25 04:51:11 +08:00
|
|
|
{
|
2016-08-16 12:23:49 +08:00
|
|
|
std::string hash_string = "pattern_fill";
|
2015-12-25 04:51:11 +08:00
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
hash_string.append(background_ ? "1" : "0");
|
|
|
|
|
|
|
|
if (background_)
|
|
|
|
{
|
|
|
|
hash_string.append(std::to_string(background_->hash()));
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_string.append(background_ ? "1" : "0");
|
|
|
|
|
|
|
|
if (background_)
|
|
|
|
{
|
|
|
|
hash_string.append(std::to_string(background_->hash()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash_string;
|
2015-12-25 04:51:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
// gradient_fill
|
|
|
|
|
|
|
|
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-08-16 12:23:49 +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-08-16 12:23:49 +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-08-16 12:23:49 +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-08-16 12:23:49 +08:00
|
|
|
return degree_;
|
|
|
|
}
|
2016-07-15 10:09:57 +08:00
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
double gradient_fill::left() const
|
|
|
|
{
|
|
|
|
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-08-16 12:23:49 +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-08-16 12:23:49 +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-08-16 12:23:49 +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-08-16 12:23:49 +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-08-16 12:23:49 +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-08-16 12:23:49 +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-08-16 12:23:49 +08:00
|
|
|
bottom_ = value;
|
|
|
|
return *this;
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
std::string gradient_fill::to_hash_string() const
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2016-08-16 12:23:49 +08:00
|
|
|
std::string hash_string = "gradient_fill";
|
|
|
|
|
|
|
|
hash_string.append(std::to_string(static_cast<std::size_t>(type_)));
|
|
|
|
hash_string.append(std::to_string(stops_.size()));
|
|
|
|
hash_string.append(std::to_string(degree_));
|
|
|
|
hash_string.append(std::to_string(left_));
|
|
|
|
hash_string.append(std::to_string(right_));
|
|
|
|
hash_string.append(std::to_string(top_));
|
|
|
|
hash_string.append(std::to_string(bottom_));
|
|
|
|
|
|
|
|
return hash_string;
|
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-08-16 12:23:49 +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-08-16 12:23:49 +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-08-16 12:23:49 +08:00
|
|
|
return stops_;
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
// fill
|
2016-07-15 10:09:57 +08:00
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
fill::fill() : type_(fill_type::pattern)
|
|
|
|
{
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
fill::fill(const xlnt::pattern_fill &pattern)
|
|
|
|
: type_(fill_type::pattern),
|
|
|
|
pattern_(pattern)
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +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-08-16 12:23:49 +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-08-16 12:23:49 +08:00
|
|
|
throw invalid_attribute();
|
2016-07-15 10:09:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return pattern_;
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
std::string fill::to_hash_string() const
|
2016-07-15 10:09:57 +08:00
|
|
|
{
|
2016-08-16 12:23:49 +08:00
|
|
|
std::string hash_string = "fill";
|
2016-07-15 10:09:57 +08:00
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
hash_string.append(std::to_string(static_cast<std::size_t>(type_)));
|
2016-07-15 10:09:57 +08:00
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
switch (type_)
|
|
|
|
{
|
|
|
|
case fill_type::pattern:
|
|
|
|
hash_string.append(std::to_string(pattern_.hash()));
|
|
|
|
break;
|
2016-07-15 10:09:57 +08:00
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
case fill_type::gradient:
|
|
|
|
hash_string.append(std::to_string(gradient_.hash()));
|
|
|
|
break;
|
2016-05-17 07:31:44 +08:00
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
} // switch (type_)
|
2016-06-20 04:30:15 +08:00
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
return hash_string;
|
2016-06-20 04:30:15 +08:00
|
|
|
}
|
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
} // namespace xlnt
|