Skip to content

Commit 8080944

Browse files
committed
Refine the color algorithm to make it align with Liquid Glass better
1 parent e614d5d commit 8080944

2 files changed

Lines changed: 60 additions & 8 deletions

File tree

TORoundedButton/TORoundedButton.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl
116116
@property (nonatomic, assign) IBInspectable CGFloat tappedTextAlpha;
117117

118118
/// Taking the default button background color apply a brightness offset for the tapped color
119-
/// (Default is -0.1f. Set 0.0 for off).
119+
/// (Default is 0.25f. Set 0.0 for off).
120120
@property (nonatomic, assign) IBInspectable CGFloat tappedTintColorBrightnessOffset;
121121

122122
/// If desired, explicity set the background color of the button when tapped (Default is nil).

TORoundedButton/TORoundedButton.m

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ - (void)_roundedButtonCommonInit TOROUNDEDBUTTON_OBJC_DIRECT {
118118
_tappedTextAlpha = (_tappedTextAlpha > FLT_EPSILON) ?: 1.0f;
119119
_tapAnimationDuration = (_tapAnimationDuration > FLT_EPSILON) ?: 0.4f;
120120
_tappedButtonScale = (_tappedButtonScale > FLT_EPSILON) ?: 0.97f;
121-
_tappedTintColorBrightnessOffset = !TORoundedButtonFloatIsZero(_tappedTintColorBrightnessOffset) ?: -0.15f;
121+
_tappedTintColorBrightnessOffset = !TORoundedButtonFloatIsZero(_tappedTintColorBrightnessOffset) ?: 0.25f;
122122
_contentInset = (UIEdgeInsets){15.0, 15.0, 15.0, 15.0};
123123
_blurStyle = UIBlurEffectStyleDark;
124124
_impactStyle = TORoundedButtonImpactStyleMedium;
@@ -669,13 +669,65 @@ - (UIColor *)_labelBackgroundColor TOROUNDEDBUTTON_OBJC_DIRECT {
669669
}
670670

671671
- (UIColor *)_brightnessAdjustedColorWithColor:(UIColor *)color amount:(CGFloat)amount TOROUNDEDBUTTON_OBJC_DIRECT {
672-
if (!color) { return nil; }
672+
// Clamp
673+
amount = fmaxf(0.0, fminf(amount, 1.0));
673674

674-
CGFloat h, s, b, a;
675-
if (![color getHue:&h saturation:&s brightness:&b alpha:&a]) { return nil; }
676-
b += amount;
677-
b = MAX(b, 0.0f); b = MIN(b, 1.0f);
678-
return [UIColor colorWithHue:h saturation:s brightness:b alpha:a];
675+
CGFloat h = 0, s = 0, b = 0, a = 0;
676+
if (![color getHue:&h saturation:&s brightness:&b alpha:&a]) {
677+
return color;
678+
}
679+
680+
// Zones:
681+
// - RED ≈ 0.0 / 1.0
682+
// - GREEN ≈ 0.33
683+
// - BLUE ≈ 0.66
684+
BOOL isRed = (h < 0.05f || h > 0.95f);
685+
BOOL isGreen = (h > 0.25f && h < 0.45f);
686+
BOOL isBlue = (h > 0.55f && h < 0.75f);
687+
688+
CGFloat hueShift = 0.0f;
689+
CGFloat satBoost = 0.0f;
690+
CGFloat brightnessAdd = 0.0f;
691+
692+
if (isRed) {
693+
// systemRed → nudge toward orange/yellow, slightly brighter, modest sat
694+
hueShift = 0.08f * amount; // towards yellow
695+
satBoost = 0.10f * amount;
696+
brightnessAdd = 0.18f * amount;
697+
}
698+
else if (isGreen) {
699+
// systemGreen → nudge toward yellow-green, clearly brighter + more vivid
700+
hueShift = -0.08f * amount; // toward yellow-green
701+
satBoost = 0.25f * amount; // push harder here
702+
brightnessAdd = 0.22f * amount; // stronger lift so it’s clearly changed
703+
}
704+
else if (isBlue) {
705+
// systemBlue → nudge toward cyan, bright + vivid
706+
hueShift = -0.06f * amount; // toward cyan
707+
satBoost = 0.18f * amount;
708+
brightnessAdd = 0.18f * amount;
709+
}
710+
else {
711+
// Everything else: mild generic brighten + hue wobble
712+
hueShift = -0.03f * amount;
713+
satBoost = 0.15f * amount;
714+
brightnessAdd = 0.18f * amount;
715+
}
716+
717+
// Apply hue shift with wrapping
718+
CGFloat newH = h + hueShift;
719+
if (newH < 0.0f) newH += 1.0f;
720+
if (newH > 1.0f) newH -= 1.0f;
721+
722+
// Brighten: constant bump + small relative lift if not already maxed
723+
CGFloat newB = b + brightnessAdd + (1.0f - b) * 0.2f * amount;
724+
newB = fminf(newB, 1.0f);
725+
726+
// Saturation boost, clamped
727+
CGFloat newS = s + satBoost;
728+
newS = fminf(newS, 1.0f);
729+
730+
return [UIColor colorWithHue:newH saturation:newS brightness:newB alpha:a];
679731
}
680732

681733
@end

0 commit comments

Comments
 (0)