我正在尝试转换此哈希颜色代码 #159424 (绿色) 以编程方式更暗和更亮。如何做到这一点,请帮忙?
使绿色更深
toDarkColor(String hashColor){
// how to convert that hash string to make green color darker?
}
让绿色更亮
toLightColor(String hashColor){
// how to convert that hash string to make green color lighter?
}
最佳答案
对于想要变暗或变亮的人Color
而不是十六进制字符串
// ranges from 0.0 to 1.0
Color darken(Color color, [double amount = .1]) {
assert(amount >= 0 && amount <= 1);
final hsl = HSLColor.fromColor(color);
final hslDark = hsl.withLightness((hsl.lightness - amount).clamp(0.0, 1.0));
return hslDark.toColor();
}
Color lighten(Color color, [double amount = .1]) {
assert(amount >= 0 && amount <= 1);
final hsl = HSLColor.fromColor(color);
final hslLight = hsl.withLightness((hsl.lightness + amount).clamp(0.0, 1.0));
return hslLight.toColor();
}
// usage
final lightRed = lighten(Colors.red);
final darkBlue = darken(Colors.blue, .3);
Live Demo
关于dart - 以编程方式使 dart 中的十六进制颜色变亮或变暗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58360989/