dart - 以编程方式使 dart 中的十六进制颜色变亮或变暗

标签 dart colors flutter-layout uicolor color-scheme

我正在尝试转换此哈希颜色代码 #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/

相关文章:

Python - 找到相似的颜色,最好的方法

php - PHP 函数 : brightness(); making RGB colors darker/brighter 需要帮助

flutter - 如何知道列表何时足够长可以滚动?

dart - 在 Flutter 中获取设备的宽度和高度

css - Dart 语言 : how to get an image from another element?

firebase - 在 Flutter 中使用 streamBuilder 时条件不起作用

java - Java中的色轮给出N个等距颜色

dart - AngularDart 和 Zurb 基金会可以合作吗?

flutter - 每次在 Flutter 中初始化时,将参数传递给 Riverpod 提供程序的有效方法是什么?

dart - flutter 自定义画家