Skip to content

Commit

Permalink
De-duplicate conversion of SharedColor to Android int value
Browse files Browse the repository at this point in the history
Summary:
Removes duplicated code in SharedColor conversions. The original copy was done for the MapBuffer experiment, as the method was returning `folly::dynamic` instead of integer. Nothing prevents us from returning integer here directly, so we can keep one implementation.

Changelog: [Internal] - Removed duplicated SharedColor conversion for Android

Reviewed By: javache

Differential Revision: D33797490

fbshipit-source-id: 196657f0616e6cb7e987225b76328fe77fd6c28a
  • Loading branch information
Andrei Shikov authored and facebook-github-bot committed Feb 9, 2022
1 parent 4e947ec commit 3112238
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
18 changes: 10 additions & 8 deletions ReactCommon/react/renderer/attributedstring/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -827,11 +827,11 @@ inline folly::dynamic toDynamic(const TextAttributes &textAttributes) {
auto _textAttributes = folly::dynamic::object();
if (textAttributes.foregroundColor) {
_textAttributes(
"foregroundColor", toDynamic(textAttributes.foregroundColor));
"foregroundColor", toAndroidRepr(textAttributes.foregroundColor));
}
if (textAttributes.backgroundColor) {
_textAttributes(
"backgroundColor", toDynamic(textAttributes.backgroundColor));
"backgroundColor", toAndroidRepr(textAttributes.backgroundColor));
}
if (!std::isnan(textAttributes.opacity)) {
_textAttributes("opacity", textAttributes.opacity);
Expand Down Expand Up @@ -876,7 +876,8 @@ inline folly::dynamic toDynamic(const TextAttributes &textAttributes) {
// Decoration
if (textAttributes.textDecorationColor) {
_textAttributes(
"textDecorationColor", toDynamic(textAttributes.textDecorationColor));
"textDecorationColor",
toAndroidRepr(textAttributes.textDecorationColor));
}
if (textAttributes.textDecorationLineType.has_value()) {
_textAttributes(
Expand All @@ -894,7 +895,7 @@ inline folly::dynamic toDynamic(const TextAttributes &textAttributes) {
}
if (textAttributes.textShadowColor) {
_textAttributes(
"textShadowColor", toDynamic(textAttributes.textShadowColor));
"textShadowColor", toAndroidRepr(textAttributes.textShadowColor));
}
// Special
if (textAttributes.isHighlighted.has_value()) {
Expand Down Expand Up @@ -1036,11 +1037,11 @@ inline MapBuffer toMapBuffer(const TextAttributes &textAttributes) {
auto builder = MapBufferBuilder();
if (textAttributes.foregroundColor) {
builder.putInt(
TA_KEY_FOREGROUND_COLOR, toMapBuffer(textAttributes.foregroundColor));
TA_KEY_FOREGROUND_COLOR, toAndroidRepr(textAttributes.foregroundColor));
}
if (textAttributes.backgroundColor) {
builder.putInt(
TA_KEY_BACKGROUND_COLOR, toMapBuffer(textAttributes.backgroundColor));
TA_KEY_BACKGROUND_COLOR, toAndroidRepr(textAttributes.backgroundColor));
}
if (!std::isnan(textAttributes.opacity)) {
builder.putDouble(TA_KEY_OPACITY, textAttributes.opacity);
Expand Down Expand Up @@ -1087,7 +1088,7 @@ inline MapBuffer toMapBuffer(const TextAttributes &textAttributes) {
if (textAttributes.textDecorationColor) {
builder.putInt(
TA_KEY_TEXT_DECORATION_COLOR,
toMapBuffer(textAttributes.textDecorationColor));
toAndroidRepr(textAttributes.textDecorationColor));
}
if (textAttributes.textDecorationLineType.has_value()) {
builder.putString(
Expand All @@ -1107,7 +1108,8 @@ inline MapBuffer toMapBuffer(const TextAttributes &textAttributes) {
}
if (textAttributes.textShadowColor) {
builder.putInt(
TA_KEY_TEXT_SHADOW_COLOR, toMapBuffer(textAttributes.textShadowColor));
TA_KEY_TEXT_SHADOW_COLOR,
toAndroidRepr(textAttributes.textShadowColor));
}
// Special
if (textAttributes.isHighlighted.has_value()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ inline folly::dynamic toDynamic(AndroidProgressBarProps const &props) {
serializedProps["indeterminate"] = props.indeterminate;
serializedProps["progress"] = props.progress;
serializedProps["animating"] = props.animating;
serializedProps["color"] = toDynamic(props.color);
serializedProps["color"] = toAndroidRepr(props.color);
serializedProps["testID"] = props.testID;
return serializedProps;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ folly::dynamic AndroidTextInputProps::getDynamic() const {
props["numberOfLines"] = numberOfLines;
props["disableFullscreenUI"] = disableFullscreenUI;
props["textBreakStrategy"] = textBreakStrategy;
props["underlineColorAndroid"] = toDynamic(underlineColorAndroid);
props["underlineColorAndroid"] = toAndroidRepr(underlineColorAndroid);
props["inlineImageLeft"] = inlineImageLeft;
props["inlineImagePadding"] = inlineImagePadding;
props["importantForAutofill"] = importantForAutofill;
Expand All @@ -282,32 +282,32 @@ folly::dynamic AndroidTextInputProps::getDynamic() const {
props["maxLength"] = maxLength;
props["multiline"] = multiline;
props["placeholder"] = placeholder;
props["placeholderTextColor"] = toDynamic(placeholderTextColor);
props["placeholderTextColor"] = toAndroidRepr(placeholderTextColor);
props["secureTextEntry"] = secureTextEntry;
props["selectionColor"] = toDynamic(selectionColor);
props["selectionColor"] = toAndroidRepr(selectionColor);
props["selection"] = toDynamic(selection);
props["value"] = value;
props["defaultValue"] = defaultValue;
props["selectTextOnFocus"] = selectTextOnFocus;
props["blurOnSubmit"] = blurOnSubmit;
props["caretHidden"] = caretHidden;
props["contextMenuHidden"] = contextMenuHidden;
props["textShadowColor"] = toDynamic(textShadowColor);
props["textShadowColor"] = toAndroidRepr(textShadowColor);
props["textShadowRadius"] = textShadowRadius;
props["textDecorationLine"] = textDecorationLine;
props["fontStyle"] = fontStyle;
props["textShadowOffset"] = toDynamic(textShadowOffset);
props["lineHeight"] = lineHeight;
props["textTransform"] = textTransform;
props["color"] = toDynamic(color);
props["color"] = toAndroidRepr(color);
props["letterSpacing"] = letterSpacing;
props["fontSize"] = fontSize;
props["textAlign"] = textAlign;
props["includeFontPadding"] = includeFontPadding;
props["fontWeight"] = fontWeight;
props["fontFamily"] = fontFamily;
props["textAlignVertical"] = textAlignVertical;
props["cursorColor"] = toDynamic(cursorColor);
props["cursorColor"] = toAndroidRepr(cursorColor);
props["mostRecentEventCount"] = mostRecentEventCount;
props["text"] = text;

Expand Down
12 changes: 1 addition & 11 deletions ReactCommon/react/renderer/graphics/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,7 @@ inline void fromRawValue(

#ifdef ANDROID

inline folly::dynamic toDynamic(const SharedColor &color) {
ColorComponents components = colorComponentsFromColor(color);
auto ratio = 255.f;
return (
((int)round(components.alpha * ratio) & 0xff) << 24 |
((int)round(components.red * ratio) & 0xff) << 16 |
((int)round(components.green * ratio) & 0xff) << 8 |
((int)round(components.blue * ratio) & 0xff));
}

inline int toMapBuffer(const SharedColor &color) {
inline int toAndroidRepr(const SharedColor &color) {
ColorComponents components = colorComponentsFromColor(color);
auto ratio = 255.f;
return (
Expand Down

0 comments on commit 3112238

Please sign in to comment.