aves/lib/utils/color_utils.dart
2020-07-26 03:03:07 +09:00

14 lines
501 B
Dart

import 'package:flutter/material.dart';
final Map<String, Color> _stringColors = {};
Color stringToColor(String string, {double saturation = .8, double lightness = .6}) {
var color = _stringColors[string];
if (color == null) {
final hash = string.codeUnits.fold<int>(0, (prev, el) => prev = el + ((prev << 5) - prev));
final hue = (hash % 360).toDouble();
color = HSLColor.fromAHSL(1.0, hue, saturation, lightness).toColor();
_stringColors[string] = color;
}
return color;
}