1+ import 'dart:math' ;
2+
13import 'package:flutter/material.dart' ;
24import 'package:lit_ui_kit/containers.dart' ;
35import 'package:lit_ui_kit/styles.dart' ;
@@ -67,13 +69,15 @@ class __UserIconState extends State<LitUserIcon> {
6769 /// Returns a stylized user color.
6870 Color get _userColor {
6971 Color uColor = widget.primaryColor;
70- Color contrastColor = Color (0xFFCDCDCD );
71- int alpha = uColor.alpha;
72- int red = (uColor.red * 0.8 ).floor ();
73- int green = (uColor.green * 0.8 ).floor ();
74- int blue = (uColor.blue * 0.8 ).floor ();
75- Color desatColor = Color .fromARGB (alpha, red, green, blue);
76- return Color .lerp (contrastColor, desatColor, 0.3 )! ;
72+ int alpha = (uColor.alpha * 0.75 ).floor ();
73+ int red = uColor.red;
74+ int green = uColor.green;
75+ int blue = uColor.blue;
76+ return Color .fromARGB (alpha, red, green, blue);
77+ }
78+
79+ Color get _contrastColor {
80+ return Color (0xFFFFFFFF );
7781 }
7882
7983 /// Returns the initials of the user derived by the username.
@@ -99,9 +103,19 @@ class __UserIconState extends State<LitUserIcon> {
99103 }
100104
101105 Color get _textColor {
102- return _userColor.computeLuminance () >= 0.5
103- ? Colors .white
104- : Color (0xFF888888 );
106+ Color textLight = Colors .white;
107+ Color textDark = Color (0xFF888888 );
108+ double luminanceUserColor = _userColor.computeLuminance ();
109+ double luminanceTextLight = textLight.computeLuminance ();
110+ double luminacneTextDark = textDark.computeLuminance ();
111+ double contrastLight = (max (luminanceUserColor, luminanceTextLight) +
112+ 0.05 / min (luminanceUserColor, luminanceTextLight) +
113+ 0.05 );
114+ double contrastDark = (max (luminanceUserColor, luminacneTextDark) +
115+ 0.05 / min (luminanceUserColor, luminacneTextDark) +
116+ 0.05 );
117+ Color textColor = (contrastDark > contrastLight) ? textDark : textLight;
118+ return textColor;
105119 }
106120
107121 void _onTap () {
@@ -126,7 +140,7 @@ class __UserIconState extends State<LitUserIcon> {
126140 end: Alignment .bottomLeft,
127141 colors: [
128142 _userColor,
129- Colors .white ,
143+ _contrastColor ,
130144 ]),
131145 borderRadius: widget.borderRadius,
132146 ),
0 commit comments