|
| 1 | +import 'dart:ui'; |
| 2 | + |
| 3 | +void main() { |
| 4 | + test(); |
| 5 | +} |
| 6 | + |
| 7 | +/// Color 类的所有 Api 完整使用示例 |
| 8 | +void test() { |
| 9 | + // 方式1:使用16进制颜色值 |
| 10 | + // #FFB5B5 粉红色 255 181 181 |
| 11 | + Color color = new Color(0xFFB5B5); |
| 12 | + int red = color.red; |
| 13 | + int green = color.green; |
| 14 | + int blue = color.blue; |
| 15 | + print("color: " + |
| 16 | + " red: " + |
| 17 | + red.toString() + |
| 18 | + " green: " + |
| 19 | + green.toString() + |
| 20 | + " blue: " + |
| 21 | + blue.toString()); // red: 255 green: 181 blue: 181 |
| 22 | + |
| 23 | + // 前两位为透明度:不透明度(00是完全透明,FF是完全不透明) |
| 24 | + Color color1 = new Color(0xFFFFB5B5); |
| 25 | + int alpha1 = color1.alpha; |
| 26 | + int red1 = color1.red; |
| 27 | + int green1 = color1.green; |
| 28 | + int blue1 = color1.blue; |
| 29 | + print("color1: " + |
| 30 | + " alpha: " + |
| 31 | + alpha1.toString() + |
| 32 | + " red: " + |
| 33 | + red1.toString() + |
| 34 | + " green: " + |
| 35 | + green1.toString() + |
| 36 | + " blue: " + |
| 37 | + blue1.toString()); // alpha: 255 red: 255 green: 181 blue: 181 |
| 38 | + |
| 39 | + // 方式2: 参数4为:不透明度(0是完全透明,255是完全不透明) |
| 40 | + Color color2 = Color.fromARGB(255, 255, 181, 181); |
| 41 | + int alpha2 = color2.alpha; |
| 42 | + int red2 = color2.red; |
| 43 | + int green2 = color2.green; |
| 44 | + int blue2 = color2.blue; |
| 45 | + print("color2: " + |
| 46 | + " alpha: " + |
| 47 | + alpha1.toString() + |
| 48 | + " red: " + |
| 49 | + red2.toString() + |
| 50 | + " green: " + |
| 51 | + green2.toString() + |
| 52 | + " blue: " + |
| 53 | + blue2.toString()); // alpha: 255 red: 255 green: 181 blue: 181 |
| 54 | + |
| 55 | + // 方式3: 参数4为:不透明度(0.0是完全透明,1.0是完全不透明) |
| 56 | + Color color3 = Color.fromRGBO(255, 181, 181, 1.0); |
| 57 | + int red3 = color3.red; |
| 58 | + int green3 = color3.green; |
| 59 | + int blue3 = color3.blue; |
| 60 | + double opacity3 = color3.opacity; |
| 61 | + print("color3: " + |
| 62 | + " red: " + |
| 63 | + red3.toString() + |
| 64 | + " green: " + |
| 65 | + green3.toString() + |
| 66 | + " blue: " + |
| 67 | + blue3.toString() + |
| 68 | + " " + |
| 69 | + opacity3.toString()); // red: 255 green: 181 blue: 181 1.0 |
| 70 | + |
| 71 | + // 方式4: 参数3:从0.0到1.0 |
| 72 | + // 算法:a + (b - a) * t |
| 73 | + // 从红色到蓝色,线性插值系数为0.5 |
| 74 | + Color color4 = Color.lerp(new Color(0x88FF0000), new Color(0x880000FF), 0.5); |
| 75 | + int alpha4 = color4.alpha; |
| 76 | + int red4 = color4.red; |
| 77 | + int green4 = color4.green; |
| 78 | + int blue4 = color4.blue; |
| 79 | + double opacity4 = color4.opacity; |
| 80 | + print("color4: " + |
| 81 | + " alpha: " + |
| 82 | + alpha4.toString() + |
| 83 | + " red: " + |
| 84 | + red4.toString() + |
| 85 | + " green: " + |
| 86 | + green4.toString() + |
| 87 | + " blue: " + |
| 88 | + blue4.toString() + |
| 89 | + " opacity:" + |
| 90 | + opacity4 |
| 91 | + .toString()); // alpha: 136 red: 127 green: 0 blue: 127 opacity:0.5333333333333333 |
| 92 | + |
| 93 | + // 方式5:把前景色作为透明色盖在背景色上面 |
| 94 | + Color color5 = Color.alphaBlend(new Color(0x88FF0000), new Color(0x880000FF)); |
| 95 | + int alpha5 = color5.alpha; |
| 96 | + int red5 = color5.red; |
| 97 | + int green5 = color5.green; |
| 98 | + int blue5 = color5.blue; |
| 99 | + double opacity5 = color4.opacity; |
| 100 | + print("color5: " + |
| 101 | + " alpha: " + |
| 102 | + alpha5.toString() + |
| 103 | + " red: " + |
| 104 | + red5.toString() + |
| 105 | + " green: " + |
| 106 | + green5.toString() + |
| 107 | + " blue: " + |
| 108 | + blue5.toString() + |
| 109 | + " opacity:" + |
| 110 | + opacity5 |
| 111 | + .toString()); // alpha: 199 red: 174 green: 0 blue: 80 opacity:0.5333333333333333 |
| 112 | + |
| 113 | + // 颜色的亮度 0是最暗,1是最亮 |
| 114 | + Color color6 = new Color(0x88FF0000); |
| 115 | + double computeLuminance = color.computeLuminance(); |
| 116 | + print("color6: " + computeLuminance.toString()); // 0.5764394295278801 |
| 117 | + |
| 118 | + // 替换掉原来颜色中的 alpha 值 |
| 119 | + color6.withAlpha(100); |
| 120 | + // 替换掉原来颜色中的 red 值 |
| 121 | + color6.withRed(100); |
| 122 | + // 替换掉原来颜色中的 green 值 |
| 123 | + color6.withGreen(100); |
| 124 | + // 替换掉原来颜色中的 blue 值 |
| 125 | + color6.withBlue(100); |
| 126 | + // 替换掉原来颜色中的 opacity 值 |
| 127 | + color6.withOpacity(0.8); |
| 128 | +} |
0 commit comments