eed04eceef
Top highlight, rim and shadow are much fainter in both themes, the highlight fades out sooner (0.55 -> 0.4), the backdrop saturation boost drops from 1.6 to 1.25 and the fill opacity rises from 0.78 to 0.82. One backdrop filter per piece and the solid high-contrast piece stay.
207 lines
6.6 KiB
Dart
207 lines
6.6 KiB
Dart
import 'dart:ui' show ImageFilter;
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
/// The look of a frosted [GlassPill]-style surface, tuned once for light and
|
|
/// dark fills: how the content behind is filtered, the soft specular
|
|
/// highlight along its top edge, its faint rim and its depth shadow.
|
|
///
|
|
/// The look is clean frosted glass, not polished metal: the highlight only
|
|
/// lifts the top of the fill a little, the rim reads as a faint edge with a
|
|
/// gentle top-to-bottom falloff (not a bevel), and the shadow is a soft lift.
|
|
///
|
|
/// Every value is chosen for a surface that keeps text and icons readable at
|
|
/// WCAG AA over whatever scrolls behind it: the fill still covers most of the
|
|
/// backdrop, and the highlight only lifts the fill's top a little.
|
|
@immutable
|
|
class GlassLook {
|
|
const GlassLook._({
|
|
required this.highlight,
|
|
required this.rimTop,
|
|
required this.rimBottom,
|
|
required this.shadow,
|
|
});
|
|
|
|
/// The look over a light fill (the light theme's raised surface).
|
|
static const GlassLook light = GlassLook._(
|
|
highlight: Color(0x2EFFFFFF),
|
|
rimTop: Color(0x59FFFFFF),
|
|
rimBottom: Color(0x26FFFFFF),
|
|
shadow: Color(0x14000000),
|
|
);
|
|
|
|
/// The look over a dark fill (the dark theme's raised surface, a dark
|
|
/// viewer's pieces).
|
|
static const GlassLook dark = GlassLook._(
|
|
highlight: Color(0x0AFFFFFF),
|
|
rimTop: Color(0x1FFFFFFF),
|
|
rimBottom: Color(0x0AFFFFFF),
|
|
shadow: Color(0x40000000),
|
|
);
|
|
|
|
/// The look for a surface filled with [fill]: dark below mid luminance.
|
|
static GlassLook forFill(Color fill) =>
|
|
fill.computeLuminance() < 0.3 ? dark : light;
|
|
|
|
/// The highlight's colour at the very top of the surface; it fades out by
|
|
/// [highlightExtent] of the surface's height.
|
|
final Color highlight;
|
|
|
|
/// The rim's colour along the top edge and along the bottom edge.
|
|
final Color rimTop;
|
|
final Color rimBottom;
|
|
|
|
/// The colour of the soft shadow under the surface.
|
|
final Color shadow;
|
|
|
|
/// How far down the surface the top highlight reaches, as a fraction of
|
|
/// its height.
|
|
static const double highlightExtent = 0.4;
|
|
|
|
/// The width of the faint rim, inside the surface's edge.
|
|
static const double rimWidth = 1;
|
|
|
|
/// The shadow's blur and how far below the surface it falls.
|
|
static const double shadowSigma = 4;
|
|
static const Offset shadowOffset = Offset(0, 1);
|
|
|
|
/// How much the backdrop's colour is boosted after blurring, so a colour
|
|
/// behind the glass still reads through its fill.
|
|
static const double saturation = 1.25;
|
|
|
|
/// The backdrop filter: a blur of [sigma], then the saturation boost. Both
|
|
/// are one filter (one backdrop read) on every renderer.
|
|
static ImageFilter backdrop(double sigma) => ImageFilter.compose(
|
|
outer: const ColorFilter.matrix(_saturationMatrix),
|
|
inner: ImageFilter.blur(sigmaX: sigma, sigmaY: sigma),
|
|
);
|
|
|
|
// A standard saturation matrix (Rec. 709 luma weights) for [saturation].
|
|
static const double _r = 0.2126 * (1 - saturation);
|
|
static const double _g = 0.7152 * (1 - saturation);
|
|
static const double _b = 0.0722 * (1 - saturation);
|
|
static const List<double> _saturationMatrix = [
|
|
_r + saturation, _g, _b, 0, 0, //
|
|
_r, _g + saturation, _b, 0, 0, //
|
|
_r, _g, _b + saturation, 0, 0, //
|
|
0, 0, 0, 1, 0, //
|
|
];
|
|
}
|
|
|
|
/// The soft specular highlight along the top of a glass surface of [shape],
|
|
/// painted over its fill and under its content.
|
|
class GlassHighlightPainter extends CustomPainter {
|
|
const GlassHighlightPainter({
|
|
required this.shape,
|
|
required this.color,
|
|
required this.textDirection,
|
|
});
|
|
|
|
final ShapeBorder shape;
|
|
final Color color;
|
|
final TextDirection? textDirection;
|
|
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
if (color.a == 0 || size.isEmpty) return;
|
|
final rect = Offset.zero & size;
|
|
final paint = Paint()
|
|
..shader = LinearGradient(
|
|
begin: Alignment.topCenter,
|
|
end: Alignment.bottomCenter,
|
|
colors: [color, color.withValues(alpha: 0)],
|
|
stops: const [0, GlassLook.highlightExtent],
|
|
).createShader(rect);
|
|
canvas.drawPath(
|
|
shape.getOuterPath(rect, textDirection: textDirection),
|
|
paint,
|
|
);
|
|
}
|
|
|
|
@override
|
|
bool shouldRepaint(GlassHighlightPainter oldDelegate) =>
|
|
color != oldDelegate.color ||
|
|
shape != oldDelegate.shape ||
|
|
textDirection != oldDelegate.textDirection;
|
|
}
|
|
|
|
/// The thin rim of a glass surface of [shape]: a faint stroke just inside
|
|
/// its edge, a little brighter along the top than along the bottom, painted over
|
|
/// the surface's content (ink included) so it stays a clean edge.
|
|
class GlassRimPainter extends CustomPainter {
|
|
const GlassRimPainter({
|
|
required this.shape,
|
|
required this.top,
|
|
required this.bottom,
|
|
required this.textDirection,
|
|
});
|
|
|
|
final ShapeBorder shape;
|
|
final Color top;
|
|
final Color bottom;
|
|
final TextDirection? textDirection;
|
|
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
if ((top.a == 0 && bottom.a == 0) || size.isEmpty) return;
|
|
const inset = GlassLook.rimWidth / 2;
|
|
final rect = (Offset.zero & size).deflate(inset);
|
|
final paint = Paint()
|
|
..style = PaintingStyle.stroke
|
|
..strokeWidth = GlassLook.rimWidth
|
|
..shader = LinearGradient(
|
|
begin: Alignment.topCenter,
|
|
end: Alignment.bottomCenter,
|
|
colors: [top, bottom, bottom],
|
|
stops: const [0, 0.5, 1],
|
|
).createShader(rect);
|
|
canvas.drawPath(
|
|
shape.getOuterPath(rect, textDirection: textDirection),
|
|
paint,
|
|
);
|
|
}
|
|
|
|
@override
|
|
bool shouldRepaint(GlassRimPainter oldDelegate) =>
|
|
top != oldDelegate.top ||
|
|
bottom != oldDelegate.bottom ||
|
|
shape != oldDelegate.shape ||
|
|
textDirection != oldDelegate.textDirection;
|
|
}
|
|
|
|
/// The soft depth shadow of a glass surface of [shape], blurred only outside
|
|
/// its outline ([BlurStyle.outer]): under a translucent fill a shadow would
|
|
/// darken the glass itself.
|
|
class GlassShadowPainter extends CustomPainter {
|
|
const GlassShadowPainter({
|
|
required this.shape,
|
|
required this.color,
|
|
required this.textDirection,
|
|
});
|
|
|
|
final ShapeBorder shape;
|
|
final Color color;
|
|
final TextDirection? textDirection;
|
|
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
if (color.a == 0 || size.isEmpty) return;
|
|
final rect = (Offset.zero & size).shift(GlassLook.shadowOffset);
|
|
canvas.drawPath(
|
|
shape.getOuterPath(rect, textDirection: textDirection),
|
|
Paint()
|
|
..color = color
|
|
..maskFilter = const MaskFilter.blur(
|
|
BlurStyle.outer,
|
|
GlassLook.shadowSigma,
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
bool shouldRepaint(GlassShadowPainter oldDelegate) =>
|
|
color != oldDelegate.color ||
|
|
shape != oldDelegate.shape ||
|
|
textDirection != oldDelegate.textDirection;
|
|
}
|