1+ import 'dart:math' show sqrt;
2+
13import 'package:flutter/material.dart' ;
24import '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