Skip to content

Commit 2f769be

Browse files
authored
Soften shadows (flutter#17231)
* Soften shadows * update goldens
1 parent 48902d1 commit 2f769be

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

lib/web_ui/dev/goldens_lock.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
repository: https://github.com/flutter/goldens.git
2-
revision: 8f692819e8881b7d2131dbd61d965c21d5e3e345
2+
revision: ae6003206eb721137c20cd56d8d1d8e2a76d6dd1

lib/web_ui/lib/src/engine/canvas_pool.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,12 @@ class _CanvasPool extends _SaveStackTracking {
613613
context.save();
614614
context.filter = 'none';
615615
context.strokeStyle = '';
616-
context.fillStyle = colorToCssString(color);
616+
final int red = color.red;
617+
final int green = color.green;
618+
final int blue = color.blue;
619+
// Multiply by 0.4 to make shadows less aggressive (https://github.com/flutter/flutter/issues/52734)
620+
final int alpha = (0.4 * color.alpha).round();
621+
context.fillStyle = colorComponentsToCssString(red, green, blue, alpha);
617622
context.shadowBlur = shadow.blurWidth;
618623
context.shadowColor = colorToCssString(color.withAlpha(0xff));
619624
context.shadowOffsetX = shadow.offset.dx;

lib/web_ui/lib/src/engine/shadow.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ void applyCssShadow(
133133
if (shadow == null) {
134134
element.style.boxShadow = 'none';
135135
} else {
136+
// Multiply by 0.4 to make shadows less aggressive (https://github.com/flutter/flutter/issues/52734)
137+
final double alpha = 0.4 * color.alpha / 255;
136138
element.style.boxShadow = '${shadow.offset.dx}px ${shadow.offset.dy}px '
137-
'${shadow.blurWidth}px 0px rgb(${color.red}, ${color.green}, ${color.blue})';
139+
'${shadow.blurWidth}px 0px rgba(${color.red}, ${color.green}, ${color.blue}, $alpha)';
138140
}
139141
}

lib/web_ui/lib/src/engine/util.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,16 @@ String _colorToCssStringRgbOnly(ui.Color color) {
349349
return '#${paddedValue.substring(paddedValue.length - 6)}';
350350
}
351351

352+
/// Converts color components to a CSS compatible attribute value.
353+
String colorComponentsToCssString(int r, int g, int b, int a) {
354+
if (a == 255) {
355+
return 'rgb($r,$g,$b)';
356+
} else {
357+
final double alphaRatio = a / 255;
358+
return 'rgba($r,$g,$b,${alphaRatio.toStringAsFixed(2)})';
359+
}
360+
}
361+
352362
/// Determines if the (dynamic) exception passed in is a NS_ERROR_FAILURE
353363
/// (from Firefox).
354364
///

lib/web_ui/test/golden_tests/engine/shadow_golden_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:web_engine_tester/golden_tester.dart';
1212

1313
import 'scuba.dart';
1414

15-
const Color _kShadowColor = Color.fromARGB(255, 255, 0, 0);
15+
const Color _kShadowColor = Color.fromARGB(255, 0, 0, 0);
1616

1717
void main() async {
1818
final Rect region = Rect.fromLTWH(0, 0, 550, 300);

0 commit comments

Comments
 (0)