Better Gradientinfo implementation.

GitOrigin-RevId: cc7a3c0a94cd2816c2090981f78ddcedd2128d1f
This commit is contained in:
levlam 2019-12-22 04:02:39 +03:00
parent 51611ae422
commit e1f1253e21
5 changed files with 84 additions and 40 deletions

View File

@ -395,7 +395,7 @@ Result<string> BackgroundManager::get_background_url(const string &name,
url += type.get_color_hex_string(); url += type.get_color_hex_string();
return url; return url;
case BackgroundType::Type::Gradient: 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; return url;
default: default:
UNREACHABLE(); UNREACHABLE();
@ -449,7 +449,7 @@ BackgroundId BackgroundManager::search_background(const string &name, Promise<Un
if (have_hyphen) { if (have_hyphen) {
int32 top_color = static_cast<int32>(hex_to_integer<uint32>(name.substr(0, hyphen_pos))); int32 top_color = static_cast<int32>(hex_to_integer<uint32>(name.substr(0, hyphen_pos)));
int32 bottom_color = static_cast<int32>(hex_to_integer<uint32>(name.substr(hyphen_pos + 1))); int32 bottom_color = static_cast<int32>(hex_to_integer<uint32>(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 { } else {
int32 color = static_cast<int32>(hex_to_integer<uint32>(name)); int32 color = static_cast<int32>(hex_to_integer<uint32>(name));
background_id = add_solid_background(color); background_id = add_solid_background(color);
@ -556,25 +556,25 @@ BackgroundId BackgroundManager::add_solid_background(int32 color, bool is_defaul
return background_id; return background_id;
} }
BackgroundId BackgroundManager::add_gradient_background(int32 top_color, int32 bottom_color) { BackgroundId BackgroundManager::add_gradient_background(const GradientInfo &gradient_info) {
return add_gradient_background(top_color, bottom_color, false, return add_gradient_background(
(top_color & 0x808080) == 0 && (bottom_color & 0x808080) == 0); 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) { bool is_dark) {
CHECK(0 <= top_color && top_color < 0x1000000); CHECK(0 <= gradient_info.top_color && gradient_info.top_color < 0x1000000);
CHECK(0 <= bottom_color && bottom_color < 0x1000000); CHECK(0 <= gradient_info.bottom_color && gradient_info.bottom_color < 0x1000000);
BackgroundId background_id((static_cast<int64>(top_color) << 24) + bottom_color + 1); BackgroundId background_id((static_cast<int64>(gradient_info.top_color) << 24) + gradient_info.bottom_color +
(1 << 24) + 1);
Background background; Background background;
background.id = background_id; background.id = background_id;
background.is_creator = true; background.is_creator = true;
background.is_default = is_default; background.is_default = is_default;
background.is_dark = is_dark; background.is_dark = is_dark;
background.type = BackgroundType(top_color, bottom_color); background.type = BackgroundType(gradient_info);
background.name = background.name = gradient_info.get_colors_hex_string();
BackgroundType::get_color_hex_string(top_color) + "-" + BackgroundType::get_color_hex_string(bottom_color);
add_background(background); add_background(background);
return background_id; return background_id;
@ -605,7 +605,7 @@ BackgroundId BackgroundManager::set_background(const td_api::InputBackground *in
return background_id; return background_id;
} }
if (type.type == BackgroundType::Type::Gradient) { 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) { if (set_background_id_[for_dark_theme] != background_id) {
set_background_id(background_id, type, for_dark_theme); 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; auto is_dark = (wallpaper->flags_ & telegram_api::wallPaperNoFile::DARK_MASK) != 0;
if ((settings->flags_ & telegram_api::wallPaperSettings::SECOND_BACKGROUND_COLOR_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 { } else {
return add_solid_background(color, is_default, is_dark); 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) { if (expected_background_id.is_valid() && id != expected_background_id) {
LOG(ERROR) << "Expected " << expected_background_id << ", but receive " << to_string(wallpaper); 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); LOG(ERROR) << "Receive " << to_string(wallpaper);
return BackgroundId(); return BackgroundId();
} }

View File

@ -108,9 +108,9 @@ class BackgroundManager : public Actor {
BackgroundId add_solid_background(int32 color, bool is_default, bool is_dark); 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); void add_background(const Background &background);

View File

@ -10,7 +10,7 @@
namespace td { namespace td {
string BackgroundType::get_color_hex_string(int32 color) { string get_color_hex_string(int32 color) {
string result; string result;
for (int i = 20; i >= 0; i -= 4) { for (int i = 20; i >= 0; i -= 4) {
result += "0123456789abcdef"[(color >> i) & 0xf]; result += "0123456789abcdef"[(color >> i) & 0xf];
@ -18,8 +18,12 @@ string BackgroundType::get_color_hex_string(int32 color) {
return result; 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 { 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) { bool operator==(const BackgroundType &lhs, const BackgroundType &rhs) {
@ -38,14 +42,21 @@ StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &t
case BackgroundType::Type::Solid: case BackgroundType::Type::Solid:
return string_builder << "type Solid[" << type.get_color_hex_string() << ']'; return string_builder << "type Solid[" << type.get_color_hex_string() << ']';
case BackgroundType::Type::Gradient: case BackgroundType::Type::Gradient:
return string_builder << "type Gradient[" << type.get_color_hex_string() << '-' return string_builder << "type Gradient[" << type.gradient.get_colors_hex_string() << ']';
<< type.get_color_hex_string(type.intensity) << ']';
default: default:
UNREACHABLE(); UNREACHABLE();
return string_builder; 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<BackgroundType> get_background_type(const td_api::BackgroundType *type) { Result<BackgroundType> get_background_type(const td_api::BackgroundType *type) {
if (type == nullptr) { if (type == nullptr) {
return Status::Error(400, "Type must not be empty"); return Status::Error(400, "Type must not be empty");
@ -73,24 +84,24 @@ Result<BackgroundType> get_background_type(const td_api::BackgroundType *type) {
if (gradient->gradient_ == nullptr) { if (gradient->gradient_ == nullptr) {
return Status::Error(400, "Gradient info must not be empty"); 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; break;
} }
default: default:
UNREACHABLE(); UNREACHABLE();
} }
if (result.type == BackgroundType::Type::Gradient) { if (!is_valid_intensity(result.intensity)) {
if (result.intensity < 0 || result.intensity > 0xFFFFFF) { return Status::Error(400, "Wrong intensity value");
return Status::Error(400, "Wrong bottom color value");
}
} else {
if (result.intensity < 0 || result.intensity > 100) {
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"); 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; return result;
} }
@ -106,14 +117,14 @@ BackgroundType get_background_type(bool is_pattern,
is_moving = (flags & telegram_api::wallPaperSettings::MOTION_MASK) != 0; is_moving = (flags & telegram_api::wallPaperSettings::MOTION_MASK) != 0;
if ((flags & telegram_api::wallPaperSettings::BACKGROUND_COLOR_MASK) != 0) { if ((flags & telegram_api::wallPaperSettings::BACKGROUND_COLOR_MASK) != 0) {
color = settings->background_color_; color = settings->background_color_;
if (color < 0 || color > 0xFFFFFF) { if (!is_valid_color(color)) {
LOG(ERROR) << "Receive " << to_string(settings); LOG(ERROR) << "Receive " << to_string(settings);
color = 0; color = 0;
} }
} }
if ((flags & telegram_api::wallPaperSettings::INTENSITY_MASK) != 0) { if ((flags & telegram_api::wallPaperSettings::INTENSITY_MASK) != 0) {
intensity = settings->intensity_; intensity = settings->intensity_;
if (intensity < 0 || intensity > 100) { if (!is_valid_intensity(intensity)) {
LOG(ERROR) << "Receive " << to_string(settings); LOG(ERROR) << "Receive " << to_string(settings);
intensity = 0; intensity = 0;
} }
@ -126,8 +137,8 @@ BackgroundType get_background_type(bool is_pattern,
} }
} }
td_api::object_ptr<td_api::gradientInfo> get_gradient_info_object(int32 top_color, int32 bottom_color) { static td_api::object_ptr<td_api::gradientInfo> get_gradient_info_object(const GradientInfo &gradient) {
return td_api::make_object<td_api::gradientInfo>(top_color, bottom_color); return td_api::make_object<td_api::gradientInfo>(gradient.top_color, gradient.bottom_color);
} }
td_api::object_ptr<td_api::BackgroundType> get_background_type_object(const BackgroundType &type) { td_api::object_ptr<td_api::BackgroundType> get_background_type_object(const BackgroundType &type) {
@ -139,7 +150,7 @@ td_api::object_ptr<td_api::BackgroundType> get_background_type_object(const Back
case BackgroundType::Type::Solid: case BackgroundType::Type::Solid:
return td_api::make_object<td_api::backgroundTypeSolid>(type.color); return td_api::make_object<td_api::backgroundTypeSolid>(type.color);
case BackgroundType::Type::Gradient: case BackgroundType::Type::Gradient:
return td_api::make_object<td_api::backgroundTypeGradient>(get_gradient_info_object(type.color, type.intensity)); return td_api::make_object<td_api::backgroundTypeGradient>(get_gradient_info_object(type.gradient));
default: default:
UNREACHABLE(); UNREACHABLE();
return nullptr; return nullptr;

View File

@ -15,6 +15,19 @@
namespace td { 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 { struct BackgroundType {
enum class Type : int32 { Wallpaper, Pattern, Solid, Gradient }; enum class Type : int32 { Wallpaper, Pattern, Solid, Gradient };
Type type = Type::Solid; Type type = Type::Solid;
@ -22,6 +35,7 @@ struct BackgroundType {
bool is_moving = false; bool is_moving = false;
int32 color = 0; int32 color = 0;
int32 intensity = 0; int32 intensity = 0;
GradientInfo gradient;
BackgroundType() = default; BackgroundType() = default;
BackgroundType(bool is_blurred, bool is_moving) BackgroundType(bool is_blurred, bool is_moving)
@ -32,13 +46,10 @@ struct BackgroundType {
} }
explicit BackgroundType(int32 color) : type(Type::Solid), color(color) { explicit BackgroundType(int32 color) : type(Type::Solid), color(color) {
} }
BackgroundType(int32 top_color, int32 bottom_color) BackgroundType(GradientInfo gradient) : type(Type::Gradient), gradient(gradient) {
: type(Type::Gradient), color(top_color), intensity(bottom_color) {
} }
string get_color_hex_string() const; string get_color_hex_string() const;
static string get_color_hex_string(int32 color);
}; };
bool operator==(const BackgroundType &lhs, const BackgroundType &rhs); bool operator==(const BackgroundType &lhs, const BackgroundType &rhs);

View File

@ -12,6 +12,22 @@
namespace td { namespace td {
template <class StorerT>
void store(const GradientInfo &gradient, StorerT &storer) {
BEGIN_STORE_FLAGS();
END_STORE_FLAGS();
store(gradient.top_color, storer);
store(gradient.bottom_color, storer);
}
template <class ParserT>
void parse(GradientInfo &gradient, ParserT &parser) {
BEGIN_PARSE_FLAGS();
END_PARSE_FLAGS();
parse(gradient.top_color, parser);
parse(gradient.bottom_color, parser);
}
template <class StorerT> template <class StorerT>
void store(const BackgroundType &type, StorerT &storer) { void store(const BackgroundType &type, StorerT &storer) {
bool has_color = type.color != 0; bool has_color = type.color != 0;
@ -29,6 +45,9 @@ void store(const BackgroundType &type, StorerT &storer) {
if (has_intensity) { if (has_intensity) {
store(type.intensity, storer); store(type.intensity, storer);
} }
if (type.type == BackgroundType::Type::Gradient) {
store(type.gradient, storer);
}
} }
template <class ParserT> template <class ParserT>
@ -48,6 +67,9 @@ void parse(BackgroundType &type, ParserT &parser) {
if (has_intensity) { if (has_intensity) {
parse(type.intensity, parser); parse(type.intensity, parser);
} }
if (type.type == BackgroundType::Type::Gradient) {
parse(type.gradient, parser);
}
} }
} // namespace td } // namespace td