Skip to content

Commit 1f5f2cf

Browse files
committed
add auto-contrast and support for textColor, inputBackground, and inputTextColor
1 parent 0707c83 commit 1f5f2cf

File tree

6 files changed

+78
-21
lines changed

6 files changed

+78
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Next
22

3+
- chore: improve survey color handling ([#233](https://github.com/PostHog/posthog-flutter/pull/233))
4+
35
- feat: add `userProperties` and `userPropertiesSetOnce` parameters to `capture()` method ([#254](https://github.com/PostHog/posthog-flutter/pull/254))
46

57
# 5.11.1

ios/Classes/PostHogDisplaySurvey+Dict.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
if let submitButtonTextColor = appearance.submitButtonTextColor {
7070
appearanceDict["submitButtonTextColor"] = submitButtonTextColor
7171
}
72+
if let textColor = appearance.textColor {
73+
appearanceDict["textColor"] = textColor
74+
}
7275
if let descriptionTextColor = appearance.descriptionTextColor {
7376
appearanceDict["descriptionTextColor"] = descriptionTextColor
7477
}
@@ -78,6 +81,12 @@
7881
if let ratingButtonActiveColor = appearance.ratingButtonActiveColor {
7982
appearanceDict["ratingButtonActiveColor"] = ratingButtonActiveColor
8083
}
84+
if let inputBackground = appearance.inputBackground {
85+
appearanceDict["inputBackground"] = inputBackground
86+
}
87+
if let inputTextColor = appearance.inputTextColor {
88+
appearanceDict["inputTextColor"] = inputTextColor
89+
}
8190
if let placeholder = appearance.placeholder {
8291
appearanceDict["placeholder"] = placeholder
8392
}

ios/posthog_flutter.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ Postog flutter plugin
2121
s.ios.dependency 'Flutter'
2222
s.osx.dependency 'FlutterMacOS'
2323

24-
# ~> Version 3.32.0 up to, but not including, 4.0.0
25-
s.dependency 'PostHog', '>= 3.32.0', '< 4.0.0'
24+
# ~> Version 3.38.0 up to, but not including, 4.0.0
25+
s.dependency 'PostHog', '>= 3.38.0', '< 4.0.0'
2626

2727
s.ios.deployment_target = '13.0'
2828
# PH iOS SDK 3.0.0 requires >= 10.15

lib/src/surveys/models/posthog_display_survey.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,12 @@ class PostHogDisplaySurvey {
100100
submitButtonColor: a['submitButtonColor'] as String?,
101101
submitButtonText: a['submitButtonText'] as String?,
102102
submitButtonTextColor: a['submitButtonTextColor'] as String?,
103+
textColor: a['textColor'] as String?,
103104
descriptionTextColor: a['descriptionTextColor'] as String?,
104105
ratingButtonColor: a['ratingButtonColor'] as String?,
105106
ratingButtonActiveColor: a['ratingButtonActiveColor'] as String?,
107+
inputBackground: a['inputBackground'] as String?,
108+
inputTextColor: a['inputTextColor'] as String?,
106109
placeholder: a['placeholder'] as String?,
107110
displayThankYouMessage: a['displayThankYouMessage'] as bool? ?? true,
108111
thankYouMessageHeader: a['thankYouMessageHeader'] as String?,

lib/src/surveys/models/posthog_display_survey_appearance.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ class PostHogDisplaySurveyAppearance {
1111
this.submitButtonColor,
1212
this.submitButtonText,
1313
this.submitButtonTextColor,
14+
this.textColor,
1415
this.descriptionTextColor,
1516
this.ratingButtonColor,
1617
this.ratingButtonActiveColor,
18+
this.inputBackground,
19+
this.inputTextColor,
1720
this.placeholder,
1821
this.displayThankYouMessage = true,
1922
this.thankYouMessageHeader,
@@ -28,9 +31,12 @@ class PostHogDisplaySurveyAppearance {
2831
final String? submitButtonColor;
2932
final String? submitButtonText;
3033
final String? submitButtonTextColor;
34+
final String? textColor;
3135
final String? descriptionTextColor;
3236
final String? ratingButtonColor;
3337
final String? ratingButtonActiveColor;
38+
final String? inputBackground;
39+
final String? inputTextColor;
3440
final String? placeholder;
3541
final bool displayThankYouMessage;
3642
final String? thankYouMessageHeader;

lib/src/surveys/models/survey_appearance.dart

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:math' show sqrt;
2+
13
import 'package:flutter/material.dart';
24
import 'posthog_display_survey_appearance.dart';
35

@@ -53,24 +55,45 @@ class SurveyAppearance {
5355
/// Creates a [SurveyAppearance] from a [PostHogDisplaySurveyAppearance]
5456
static SurveyAppearance fromPostHog(
5557
PostHogDisplaySurveyAppearance? appearance) {
58+
final backgroundColor =
59+
_colorFromHex(appearance?.backgroundColor) ?? Colors.white;
60+
final submitButtonColor =
61+
_colorFromHex(appearance?.submitButtonColor) ?? Colors.black;
62+
final ratingButtonColor =
63+
_colorFromHex(appearance?.ratingButtonColor) ?? const Color(0xFFEEEEEE);
64+
final ratingButtonActiveColor =
65+
_colorFromHex(appearance?.ratingButtonActiveColor) ?? Colors.black;
66+
67+
// Input background: use override, or slight adjustment for white backgrounds
68+
final inputBackgroundColor = _colorFromHex(appearance?.inputBackground) ??
69+
(backgroundColor == Colors.white
70+
? const Color(0xFFF8F8F8)
71+
: backgroundColor);
72+
73+
// Primary text color: use textColor override if provided, otherwise auto-contrast
74+
final primaryTextColor = _colorFromHex(appearance?.textColor) ??
75+
_getContrastingTextColor(backgroundColor);
76+
77+
// Input text color: use override if provided, otherwise auto-contrast from input background
78+
final inputTextColor = _colorFromHex(appearance?.inputTextColor) ??
79+
_getContrastingTextColor(inputBackgroundColor);
80+
5681
return SurveyAppearance(
57-
backgroundColor:
58-
_colorFromHex(appearance?.backgroundColor) ?? Colors.white,
59-
submitButtonColor:
60-
_colorFromHex(appearance?.submitButtonColor) ?? Colors.black,
82+
backgroundColor: backgroundColor,
83+
submitButtonColor: submitButtonColor,
6184
submitButtonText: appearance?.submitButtonText ?? 'Submit',
6285
submitButtonTextColor:
63-
_colorFromHex(appearance?.submitButtonTextColor) ?? Colors.white,
86+
_colorFromHex(appearance?.submitButtonTextColor) ??
87+
_getContrastingTextColor(submitButtonColor),
6488
descriptionTextColor:
65-
_colorFromHex(appearance?.descriptionTextColor) ?? Colors.black,
66-
questionTextColor: Colors.black,
67-
closeButtonColor: Colors.black,
68-
ratingButtonColor: _colorFromHex(appearance?.ratingButtonColor) ??
69-
const Color(0xFFEEEEEE),
70-
ratingButtonActiveColor:
71-
_colorFromHex(appearance?.ratingButtonActiveColor) ?? Colors.black,
72-
ratingButtonSelectedTextColor: Colors.white,
73-
ratingButtonUnselectedTextColor: const Color(0x80000000),
89+
_colorFromHex(appearance?.descriptionTextColor) ?? primaryTextColor,
90+
questionTextColor: primaryTextColor,
91+
closeButtonColor: primaryTextColor,
92+
ratingButtonColor: ratingButtonColor,
93+
ratingButtonActiveColor: ratingButtonActiveColor,
94+
ratingButtonSelectedTextColor:
95+
_getContrastingTextColor(ratingButtonActiveColor),
96+
ratingButtonUnselectedTextColor: inputTextColor.withAlpha(128),
7497
displayThankYouMessage: appearance?.displayThankYouMessage ?? true,
7598
thankYouMessageHeader:
7699
appearance?.thankYouMessageHeader ?? 'Thank you for your feedback!',
@@ -79,14 +102,28 @@ class SurveyAppearance {
79102
appearance?.thankYouMessageCloseButtonText ?? 'Close',
80103
borderColor:
81104
_colorFromHex(appearance?.borderColor) ?? const Color(0xFFBDBDBD),
82-
inputBackgroundColor: Colors.white,
83-
inputTextColor: Colors.black,
84-
inputPlaceholderColor: const Color(0xFF757575),
85-
choiceButtonBorderColor: Colors.black,
86-
choiceButtonTextColor: Colors.black,
105+
inputBackgroundColor: inputBackgroundColor,
106+
inputTextColor: inputTextColor,
107+
inputPlaceholderColor: inputTextColor.withAlpha(153),
108+
choiceButtonBorderColor: primaryTextColor,
109+
choiceButtonTextColor: primaryTextColor,
87110
);
88111
}
89112

113+
/// Returns black or white text color based on the perceived brightness of the background.
114+
/// Uses the HSP (Highly Sensitive Perceived) color model for perceived brightness.
115+
/// This matches the algorithm used in posthog-js.
116+
static Color _getContrastingTextColor(Color color) {
117+
final r = color.red;
118+
final g = color.green;
119+
final b = color.blue;
120+
// HSP equation for perceived brightness
121+
final hsp =
122+
sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b));
123+
// Using 127.5 as threshold (same as JS)
124+
return hsp > 127.5 ? Colors.black : Colors.white;
125+
}
126+
90127
static Color? _colorFromHex(String? colorString) {
91128
if (colorString == null || colorString.isEmpty) return null;
92129

0 commit comments

Comments
 (0)