Skip to content

Commit 1e74336

Browse files
authored
fix: do not add opacity for transparent background color (#162)
Fixes: #161
1 parent 096e2f7 commit 1e74336

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ class HtmlStyle {
154154
}
155155

156156
private fun withOpacity(color: Int, alpha: Int): Int {
157+
// Do not apply opacity to transparent color
158+
if (Color.alpha(color) == 0) return color
157159
val a = alpha.coerceIn(0, 255)
158160
return (color and 0x00FFFFFF) or (a shl 24)
159161
}

ios/styles/InlineCodeStyle.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#import "FontExtension.h"
44
#import "OccurenceUtils.h"
55
#import "ParagraphsUtils.h"
6+
#import "ColorExtension.h"
67

78
@implementation InlineCodeStyle {
89
EnrichedTextInputView *_input;
@@ -33,7 +34,7 @@ - (void)addAttributes:(NSRange)range {
3334
NSRange currentRange = [value rangeValue];
3435
[_input->textView.textStorage beginEditing];
3536

36-
[_input->textView.textStorage addAttribute:NSBackgroundColorAttributeName value:[[_input->config inlineCodeBgColor] colorWithAlphaComponent:0.4] range:currentRange];
37+
[_input->textView.textStorage addAttribute:NSBackgroundColorAttributeName value:[[_input->config inlineCodeBgColor] colorWithAlphaIfNotTransparent:0.4] range:currentRange];
3738
[_input->textView.textStorage addAttribute:NSForegroundColorAttributeName value:[_input->config inlineCodeFgColor] range:currentRange];
3839
[_input->textView.textStorage addAttribute:NSUnderlineColorAttributeName value:[_input->config inlineCodeFgColor] range:currentRange];
3940
[_input->textView.textStorage addAttribute:NSStrikethroughColorAttributeName value:[_input->config inlineCodeFgColor] range:currentRange];
@@ -53,7 +54,7 @@ - (void)addAttributes:(NSRange)range {
5354

5455
- (void)addTypingAttributes {
5556
NSMutableDictionary *newTypingAttrs = [_input->textView.typingAttributes mutableCopy];
56-
newTypingAttrs[NSBackgroundColorAttributeName] = [[_input->config inlineCodeBgColor] colorWithAlphaComponent:0.4];
57+
newTypingAttrs[NSBackgroundColorAttributeName] = [[_input->config inlineCodeBgColor] colorWithAlphaIfNotTransparent:0.4];
5758
newTypingAttrs[NSForegroundColorAttributeName] = [_input->config inlineCodeFgColor];
5859
newTypingAttrs[NSUnderlineColorAttributeName] = [_input->config inlineCodeFgColor];
5960
newTypingAttrs[NSStrikethroughColorAttributeName] = [_input->config inlineCodeFgColor];

ios/styles/MentionStyle.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#import "TextInsertionUtils.h"
55
#import "WordsUtils.h"
66
#import "UIView+React.h"
7+
#import "ColorExtension.h"
78

89
// custom NSAttributedStringKey to differentiate from links
910
static NSString *const MentionAttributeName = @"MentionAttributeName";
@@ -154,7 +155,7 @@ - (void)addMention:(NSString *)indicator text:(NSString *)text attributes:(NSStr
154155
NSForegroundColorAttributeName: styleProps.color,
155156
NSUnderlineColorAttributeName: styleProps.color,
156157
NSStrikethroughColorAttributeName: styleProps.color,
157-
NSBackgroundColorAttributeName: [styleProps.backgroundColor colorWithAlphaComponent:0.4],
158+
NSBackgroundColorAttributeName: [styleProps.backgroundColor colorWithAlphaIfNotTransparent:0.4],
158159
} mutableCopy];
159160

160161
if(styleProps.decorationLine == DecorationUnderline) {
@@ -186,7 +187,7 @@ - (void)addMentionAtRange:(NSRange)range params:(MentionParams *)params {
186187
NSForegroundColorAttributeName: styleProps.color,
187188
NSUnderlineColorAttributeName: styleProps.color,
188189
NSStrikethroughColorAttributeName: styleProps.color,
189-
NSBackgroundColorAttributeName: [styleProps.backgroundColor colorWithAlphaComponent:0.4],
190+
NSBackgroundColorAttributeName: [styleProps.backgroundColor colorWithAlphaIfNotTransparent:0.4],
190191
} mutableCopy];
191192

192193
if(styleProps.decorationLine == DecorationUnderline) {

ios/utils/ColorExtension.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33

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

ios/utils/ColorExtension.mm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,13 @@ - (BOOL)isEqualToColor:(UIColor *)otherColor {
2424

2525
return [selfColor isEqual:otherColor];
2626
}
27+
28+
- (UIColor *)colorWithAlphaIfNotTransparent:(CGFloat)newAlpha {
29+
CGFloat alpha = 0.0;
30+
[self getRed:nil green:nil blue:nil alpha:&alpha];
31+
if (alpha > 0.0) {
32+
return [self colorWithAlphaComponent:newAlpha];
33+
}
34+
return self;
35+
}
2736
@end

0 commit comments

Comments
 (0)