Skip to content

Commit

Permalink
猫耳の色をアイコン画像の平均色に変更
Browse files Browse the repository at this point in the history
  • Loading branch information
poppingmoon committed Sep 20, 2023
1 parent ca7ecb3 commit a077405
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions lib/view/common/avatar_icon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class AvatarIcon extends StatelessWidget {
id: response.id,
username: response.username,
avatarUrl: response.avatarUrl,
avatarBlurhash: response.avatarBlurhash,
isCat: response.isCat,
isBot: response.isBot,
),
Expand All @@ -41,23 +42,41 @@ class AvatarIcon extends StatelessWidget {
id: response.id,
username: response.username,
avatarUrl: response.avatarUrl,
avatarBlurhash: response.avatarBlurhash,
isCat: response.isCat,
isBot: response.isBot,
),
height: height,
);
}

Color? averageColor() {
// https://github.com/woltapp/blurhash/blob/master/Algorithm.md
final blurhash = user.avatarBlurhash;
if (blurhash == null) {
return null;
}
final value = blurhash
.substring(2, 6)
.split("")
.map(
r'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~'
.indexOf,
)
.fold(0, (acc, i) => acc * 83 + i);
return Color(0xFF000000 | value);
}

@override
Widget build(BuildContext context) {
final catEarColor =
(user.isCat ? averageColor() : null) ?? Theme.of(context).primaryColor;

return GestureDetector(
onTap: onTap ??
() {
context.pushRoute(
UserRoute(
userId: user.id,
account: AccountScope.of(context),
),
UserRoute(userId: user.id, account: AccountScope.of(context)),
);
},
child: Padding(
Expand All @@ -79,7 +98,7 @@ class AvatarIcon extends StatelessWidget {
),
child: Icon(
Icons.play_arrow_rounded,
color: Theme.of(context).primaryColor,
color: catEarColor,
size: height * 1 * MediaQuery.of(context).textScaleFactor,
),
),
Expand All @@ -100,7 +119,7 @@ class AvatarIcon extends StatelessWidget {
transform: Matrix4.rotationY(pi),
child: Icon(
Icons.play_arrow_rounded,
color: Theme.of(context).primaryColor,
color: catEarColor,
size: height * 1 * MediaQuery.of(context).textScaleFactor,
),
),
Expand Down

0 comments on commit a077405

Please sign in to comment.