From e1f1253e21fdf4983b57b0bf86d85358a3ca3a0d Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 22 Dec 2019 04:02:39 +0300 Subject: [PATCH] Better Gradientinfo implementation. GitOrigin-RevId: cc7a3c0a94cd2816c2090981f78ddcedd2128d1f --- td/telegram/BackgroundManager.cpp | 30 +++++++++---------- td/telegram/BackgroundManager.h | 4 +-- td/telegram/BackgroundType.cpp | 49 +++++++++++++++++++------------ td/telegram/BackgroundType.h | 19 +++++++++--- td/telegram/BackgroundType.hpp | 22 ++++++++++++++ 5 files changed, 84 insertions(+), 40 deletions(-) diff --git a/td/telegram/BackgroundManager.cpp b/td/telegram/BackgroundManager.cpp index b96763a8..ac147227 100644 --- a/td/telegram/BackgroundManager.cpp +++ b/td/telegram/BackgroundManager.cpp @@ -395,7 +395,7 @@ Result BackgroundManager::get_background_url(const string &name, url += type.get_color_hex_string(); return url; case BackgroundType::Type::Gradient: - url += type.get_color_hex_string() + '-' + BackgroundType::get_color_hex_string(type.intensity); + url += type.gradient.get_colors_hex_string(); return url; default: UNREACHABLE(); @@ -449,7 +449,7 @@ BackgroundId BackgroundManager::search_background(const string &name, Promise(hex_to_integer(name.substr(0, hyphen_pos))); int32 bottom_color = static_cast(hex_to_integer(name.substr(hyphen_pos + 1))); - background_id = add_gradient_background(top_color, bottom_color); + background_id = add_gradient_background(GradientInfo(top_color, bottom_color)); } else { int32 color = static_cast(hex_to_integer(name)); background_id = add_solid_background(color); @@ -556,25 +556,25 @@ BackgroundId BackgroundManager::add_solid_background(int32 color, bool is_defaul return background_id; } -BackgroundId BackgroundManager::add_gradient_background(int32 top_color, int32 bottom_color) { - return add_gradient_background(top_color, bottom_color, false, - (top_color & 0x808080) == 0 && (bottom_color & 0x808080) == 0); +BackgroundId BackgroundManager::add_gradient_background(const GradientInfo &gradient_info) { + return add_gradient_background( + gradient_info, false, (gradient_info.top_color & 0x808080) == 0 && (gradient_info.bottom_color & 0x808080) == 0); } -BackgroundId BackgroundManager::add_gradient_background(int32 top_color, int32 bottom_color, bool is_default, +BackgroundId BackgroundManager::add_gradient_background(const GradientInfo &gradient_info, bool is_default, bool is_dark) { - CHECK(0 <= top_color && top_color < 0x1000000); - CHECK(0 <= bottom_color && bottom_color < 0x1000000); - BackgroundId background_id((static_cast(top_color) << 24) + bottom_color + 1); + CHECK(0 <= gradient_info.top_color && gradient_info.top_color < 0x1000000); + CHECK(0 <= gradient_info.bottom_color && gradient_info.bottom_color < 0x1000000); + BackgroundId background_id((static_cast(gradient_info.top_color) << 24) + gradient_info.bottom_color + + (1 << 24) + 1); Background background; background.id = background_id; background.is_creator = true; background.is_default = is_default; background.is_dark = is_dark; - background.type = BackgroundType(top_color, bottom_color); - background.name = - BackgroundType::get_color_hex_string(top_color) + "-" + BackgroundType::get_color_hex_string(bottom_color); + background.type = BackgroundType(gradient_info); + background.name = gradient_info.get_colors_hex_string(); add_background(background); return background_id; @@ -605,7 +605,7 @@ BackgroundId BackgroundManager::set_background(const td_api::InputBackground *in return background_id; } if (type.type == BackgroundType::Type::Gradient) { - auto background_id = add_gradient_background(type.color, type.intensity); + auto background_id = add_gradient_background(type.gradient); if (set_background_id_[for_dark_theme] != background_id) { set_background_id(background_id, type, for_dark_theme); } @@ -969,7 +969,7 @@ BackgroundId BackgroundManager::on_get_background(BackgroundId expected_backgrou auto is_dark = (wallpaper->flags_ & telegram_api::wallPaperNoFile::DARK_MASK) != 0; if ((settings->flags_ & telegram_api::wallPaperSettings::SECOND_BACKGROUND_COLOR_MASK) != 0) { - return add_gradient_background(color, settings->second_background_color_, is_default, is_dark); + return add_gradient_background(GradientInfo(color, settings->second_background_color_), is_default, is_dark); } else { return add_solid_background(color, is_default, is_dark); } @@ -984,7 +984,7 @@ BackgroundId BackgroundManager::on_get_background(BackgroundId expected_backgrou if (expected_background_id.is_valid() && id != expected_background_id) { LOG(ERROR) << "Expected " << expected_background_id << ", but receive " << to_string(wallpaper); } - if (wallpaper->slug_.size() <= 13 || (0 < wallpaper->id_ && wallpaper->id_ <= 0x1000000000000)) { + if (wallpaper->slug_.size() <= 13 || (0 < wallpaper->id_ && wallpaper->id_ <= 0x1000001000000)) { LOG(ERROR) << "Receive " << to_string(wallpaper); return BackgroundId(); } diff --git a/td/telegram/BackgroundManager.h b/td/telegram/BackgroundManager.h index 52166555..c8c32494 100644 --- a/td/telegram/BackgroundManager.h +++ b/td/telegram/BackgroundManager.h @@ -108,9 +108,9 @@ class BackgroundManager : public Actor { BackgroundId add_solid_background(int32 color, bool is_default, bool is_dark); - BackgroundId add_gradient_background(int32 top_color, int32 bottom_color); + BackgroundId add_gradient_background(const GradientInfo &gradient_info); - BackgroundId add_gradient_background(int32 top_color, int32 bottom_color, bool is_default, bool is_dark); + BackgroundId add_gradient_background(const GradientInfo &gradient_info, bool is_default, bool is_dark); void add_background(const Background &background); diff --git a/td/telegram/BackgroundType.cpp b/td/telegram/BackgroundType.cpp index 6d07357a..af1143a5 100644 --- a/td/telegram/BackgroundType.cpp +++ b/td/telegram/BackgroundType.cpp @@ -10,7 +10,7 @@ namespace td { -string BackgroundType::get_color_hex_string(int32 color) { +string get_color_hex_string(int32 color) { string result; for (int i = 20; i >= 0; i -= 4) { result += "0123456789abcdef"[(color >> i) & 0xf]; @@ -18,8 +18,12 @@ string BackgroundType::get_color_hex_string(int32 color) { return result; } +string GradientInfo::get_colors_hex_string() const { + return PSTRING() << get_color_hex_string(top_color) << '-' << get_color_hex_string(bottom_color); +} + string BackgroundType::get_color_hex_string() const { - return get_color_hex_string(color); + return td::get_color_hex_string(color); } bool operator==(const BackgroundType &lhs, const BackgroundType &rhs) { @@ -38,14 +42,21 @@ StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &t case BackgroundType::Type::Solid: return string_builder << "type Solid[" << type.get_color_hex_string() << ']'; case BackgroundType::Type::Gradient: - return string_builder << "type Gradient[" << type.get_color_hex_string() << '-' - << type.get_color_hex_string(type.intensity) << ']'; + return string_builder << "type Gradient[" << type.gradient.get_colors_hex_string() << ']'; default: UNREACHABLE(); return string_builder; } } +static bool is_valid_color(int32 color) { + return 0 <= color && color <= 0xFFFFFF; +} + +static bool is_valid_intensity(int32 intensity) { + return 0 <= intensity && intensity <= 100; +} + Result get_background_type(const td_api::BackgroundType *type) { if (type == nullptr) { return Status::Error(400, "Type must not be empty"); @@ -73,24 +84,24 @@ Result get_background_type(const td_api::BackgroundType *type) { if (gradient->gradient_ == nullptr) { return Status::Error(400, "Gradient info must not be empty"); } - result = BackgroundType(gradient->gradient_->top_color_, gradient->gradient_->bottom_color_); + result = BackgroundType(GradientInfo(gradient->gradient_->top_color_, gradient->gradient_->bottom_color_)); break; } default: UNREACHABLE(); } - if (result.type == BackgroundType::Type::Gradient) { - if (result.intensity < 0 || result.intensity > 0xFFFFFF) { - return Status::Error(400, "Wrong bottom color value"); - } - } else { - if (result.intensity < 0 || result.intensity > 100) { - return Status::Error(400, "Wrong intensity value"); - } + if (!is_valid_intensity(result.intensity)) { + return Status::Error(400, "Wrong intensity value"); } - if (result.color < 0 || result.color > 0xFFFFFF) { + if (!is_valid_color(result.color)) { return Status::Error(400, "Wrong color value"); } + if (!is_valid_color(result.gradient.top_color)) { + return Status::Error(400, "Wrong top color value"); + } + if (!is_valid_color(result.gradient.bottom_color)) { + return Status::Error(400, "Wrong bottom color value"); + } return result; } @@ -106,14 +117,14 @@ BackgroundType get_background_type(bool is_pattern, is_moving = (flags & telegram_api::wallPaperSettings::MOTION_MASK) != 0; if ((flags & telegram_api::wallPaperSettings::BACKGROUND_COLOR_MASK) != 0) { color = settings->background_color_; - if (color < 0 || color > 0xFFFFFF) { + if (!is_valid_color(color)) { LOG(ERROR) << "Receive " << to_string(settings); color = 0; } } if ((flags & telegram_api::wallPaperSettings::INTENSITY_MASK) != 0) { intensity = settings->intensity_; - if (intensity < 0 || intensity > 100) { + if (!is_valid_intensity(intensity)) { LOG(ERROR) << "Receive " << to_string(settings); intensity = 0; } @@ -126,8 +137,8 @@ BackgroundType get_background_type(bool is_pattern, } } -td_api::object_ptr get_gradient_info_object(int32 top_color, int32 bottom_color) { - return td_api::make_object(top_color, bottom_color); +static td_api::object_ptr get_gradient_info_object(const GradientInfo &gradient) { + return td_api::make_object(gradient.top_color, gradient.bottom_color); } td_api::object_ptr get_background_type_object(const BackgroundType &type) { @@ -139,7 +150,7 @@ td_api::object_ptr get_background_type_object(const Back case BackgroundType::Type::Solid: return td_api::make_object(type.color); case BackgroundType::Type::Gradient: - return td_api::make_object(get_gradient_info_object(type.color, type.intensity)); + return td_api::make_object(get_gradient_info_object(type.gradient)); default: UNREACHABLE(); return nullptr; diff --git a/td/telegram/BackgroundType.h b/td/telegram/BackgroundType.h index 6b8af84b..f8e333d1 100644 --- a/td/telegram/BackgroundType.h +++ b/td/telegram/BackgroundType.h @@ -15,6 +15,19 @@ namespace td { +string get_color_hex_string(int32 color); + +struct GradientInfo { + int32 top_color = 0; + int32 bottom_color = 0; + + GradientInfo() = default; + GradientInfo(int32 top_color, int32 bottom_color) : top_color(top_color), bottom_color(bottom_color) { + } + + string get_colors_hex_string() const; +}; + struct BackgroundType { enum class Type : int32 { Wallpaper, Pattern, Solid, Gradient }; Type type = Type::Solid; @@ -22,6 +35,7 @@ struct BackgroundType { bool is_moving = false; int32 color = 0; int32 intensity = 0; + GradientInfo gradient; BackgroundType() = default; BackgroundType(bool is_blurred, bool is_moving) @@ -32,13 +46,10 @@ struct BackgroundType { } explicit BackgroundType(int32 color) : type(Type::Solid), color(color) { } - BackgroundType(int32 top_color, int32 bottom_color) - : type(Type::Gradient), color(top_color), intensity(bottom_color) { + BackgroundType(GradientInfo gradient) : type(Type::Gradient), gradient(gradient) { } string get_color_hex_string() const; - - static string get_color_hex_string(int32 color); }; bool operator==(const BackgroundType &lhs, const BackgroundType &rhs); diff --git a/td/telegram/BackgroundType.hpp b/td/telegram/BackgroundType.hpp index 6f1aea1c..7813e151 100644 --- a/td/telegram/BackgroundType.hpp +++ b/td/telegram/BackgroundType.hpp @@ -12,6 +12,22 @@ namespace td { +template +void store(const GradientInfo &gradient, StorerT &storer) { + BEGIN_STORE_FLAGS(); + END_STORE_FLAGS(); + store(gradient.top_color, storer); + store(gradient.bottom_color, storer); +} + +template +void parse(GradientInfo &gradient, ParserT &parser) { + BEGIN_PARSE_FLAGS(); + END_PARSE_FLAGS(); + parse(gradient.top_color, parser); + parse(gradient.bottom_color, parser); +} + template void store(const BackgroundType &type, StorerT &storer) { bool has_color = type.color != 0; @@ -29,6 +45,9 @@ void store(const BackgroundType &type, StorerT &storer) { if (has_intensity) { store(type.intensity, storer); } + if (type.type == BackgroundType::Type::Gradient) { + store(type.gradient, storer); + } } template @@ -48,6 +67,9 @@ void parse(BackgroundType &type, ParserT &parser) { if (has_intensity) { parse(type.intensity, parser); } + if (type.type == BackgroundType::Type::Gradient) { + parse(type.gradient, parser); + } } } // namespace td