Skip to content

Commit 7f4f1bc

Browse files
authored
feat: adjust opacity settings (#631)
# Summary Fixes: #627 Do not apply additional opacity if user specified it manually. ## Test Plan First scenario: - Run the app - Adjust `htmlStyle` to use color with opacity - Notice additional opacity is not applied Second scenario: - Run the app - Adjust `htmlStyle` to use color without opacity - Notice that additional opacity is applied ## Screenshots / Videos n/a ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ✅ | | Android | ✅ | ## Checklist - [x] E2E tests are passing - [ ] Required E2E tests have been added (if applicable)
1 parent 74d3bd4 commit 7f4f1bc

8 files changed

Lines changed: 20 additions & 11 deletions

File tree

android/src/main/java/com/swmansion/enriched/text/EnrichedTextStyle.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ data class EnrichedTextStyle(
155155
opacity: Int,
156156
): Int {
157157
val color = parseColor(context, map, key)
158-
if (Color.alpha(color) == 0) return color
158+
if (Color.alpha(color) != 255) return color
159159
return (color and 0x00FFFFFF) or (opacity.coerceIn(0, 255) shl 24)
160160
}
161161

android/src/main/java/com/swmansion/enriched/textinput/styles/HtmlStyle.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ class HtmlStyle : EnrichedStyle {
197197
color: Int,
198198
alpha: Int,
199199
): Int {
200-
// Do not apply opacity to transparent color
201-
if (Color.alpha(color) == 0) return color
200+
if (Color.alpha(color) != 255) return color
202201
val a = alpha.coerceIn(0, 255)
203202
return (color and 0x00FFFFFF) or (a shl 24)
204203
}

ios/extensions/ColorExtension.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
@interface UIColor (ColorExtension)
55
- (BOOL)isEqualToColor:(UIColor *)otherColor;
6-
- (UIColor *)colorWithAlphaIfNotTransparent:(CGFloat)newAlpha;
6+
- (UIColor *)colorWithResolvedAlpha;
7+
- (UIColor *)colorWithResolvedAlpha:(CGFloat)newAlpha;
78
@end

ios/extensions/ColorExtension.mm

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,20 @@ - (BOOL)isEqualToColor:(UIColor *)otherColor {
2727
return [selfColor isEqual:otherColor];
2828
}
2929

30-
- (UIColor *)colorWithAlphaIfNotTransparent:(CGFloat)newAlpha {
30+
// Default fallback alpha of 0.4 — opaque colors get this applied so the text
31+
// cursor remains visible through the highlight. If the caller already set a
32+
// custom alpha (anything below 1.0), we respect that value instead.
33+
- (UIColor *)colorWithResolvedAlpha {
34+
return [self colorWithResolvedAlpha:0.4];
35+
}
36+
37+
- (UIColor *)colorWithResolvedAlpha:(CGFloat)newAlpha {
3138
CGFloat alpha = 0.0;
3239
[self getRed:nil green:nil blue:nil alpha:&alpha];
33-
if (alpha > 0.0) {
40+
// alpha == 1.0 means the color was never given a custom transparency, so
41+
// apply newAlpha. Any other value means the caller explicitly chose it —
42+
// leave it untouched.
43+
if (alpha >= 1.0) {
3444
return [self colorWithAlphaComponent:newAlpha];
3545
}
3646
return self;

ios/extensions/LayoutManagerExtension.mm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ - (void)drawCodeBlocks:(id<EnrichedViewHost>)host
7373
NSArray<StylePair *> *allCodeBlocks = [codeBlockStyle all:visibleCharRange];
7474
NSArray<StylePair *> *mergedCodeBlocks =
7575
[self mergeContiguousStylePairs:allCodeBlocks];
76-
UIColor *bgColor =
77-
[[host.config codeBlockBgColor] colorWithAlphaIfNotTransparent:0.4];
76+
UIColor *bgColor = [[host.config codeBlockBgColor] colorWithResolvedAlpha];
7877
CGFloat radius = [host.config codeBlockBorderRadius];
7978
[bgColor setFill];
8079

ios/styles/InlineCodeStyle.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ - (void)applyStyling:(NSRange)range {
2929
[self.host.textView.textStorage
3030
addAttribute:NSBackgroundColorAttributeName
3131
value:[[self.host.config inlineCodeBgColor]
32-
colorWithAlphaIfNotTransparent:0.4]
32+
colorWithResolvedAlpha]
3333
range:subRange];
3434
[self.host.textView.textStorage
3535
addAttribute:NSForegroundColorAttributeName

ios/styles/MentionStyle.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ - (void)applyStyling:(NSRange)range {
5454
NSUnderlineColorAttributeName : styleProps.color,
5555
NSStrikethroughColorAttributeName : styleProps.color,
5656
NSBackgroundColorAttributeName :
57-
[styleProps.backgroundColor colorWithAlphaIfNotTransparent:0.4],
57+
[styleProps.backgroundColor colorWithResolvedAlpha],
5858
} mutableCopy];
5959

6060
if (styleProps.decorationLine == DecorationUnderline) {

ios/utils/EnrichedTextTouchHandler.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ - (void)updateVisualsPressed:(BOOL)pressed {
115115
}
116116
if (bgColor) {
117117
newAttrs[NSBackgroundColorAttributeName] =
118-
[bgColor colorWithAlphaIfNotTransparent:0.4];
118+
[bgColor colorWithResolvedAlpha];
119119
}
120120

121121
[self.view.textView.textStorage addAttributes:newAttrs range:_activeRange];

0 commit comments

Comments
 (0)