Flutter之Color对象(译)

Color对象

color是一个ARGB(A透明度,R就是red,G是gree,B是blue,RGB是光学三原色)格式的不可变的32位值.css

例如flutter的logo,是彻底不透明的,red部分值是0x42 (66),gree部分值是0xA5 (165),blue部分值是0xF5 (245)。在颜色值通用“哈希语法”中,它能够写为#42A5F5。html

接下来是一些写法api

Color c = const Color(0xFF42A5F5);
Color c = const Color.fromARGB(0xFF, 0x42, 0xA5, 0xF5);
Color c = const Color.fromARGB(255, 66, 165, 245);
Color c = const Color.fromRGBO(66, 165, 245, 1.0);复制代码

若是在使用color时遇到没有渲染出来的问题,检查一下颜色值是8位16进制仍是6位16进制。若是是6位16进制,会默认在前面补两个0,这样这个颜色就是彻底透明了。bash

Color c1 = const Color(0xFFFFFF); // fully transparent white (invisible)  彻底透明
Color c2 = const Color(0xFFFFFFFF); // fully opaque white (visible) 彻底不透明复制代码

同时colors里面定义了一些特殊的颜色类型ide


Implementers: 函数

  • ColorSwatch
  • MaterialStateColor

  • 构造函数:

    Color(int value)                                                                                                                ui

    int的低32位做为参数来构造颜色                                                                                        constspa

    Color.fromARGB(int a, int r, int g, int b)                                                                           code

    用4个 int型的低8位整数做为参数来构造颜色                                                                          consthtm

    Color.fromRGBO(int r, int g, int b, double opacity)                                                            

    用red,gree,blue,opacity(0到1的小数)做为参数来建立一个颜色对象,相似于css中rgba( )        const 


    属性:

    alphaint :透明度的值,只读

    blueint:blue颜色的值,只读

    greenint:gree颜色的值,只读

    hashCodeint:hash code 的值,只读, override

    opacitydouble:透明度的小数类型的值,只读

    redint:red颜色的值,只读

    valueint:当前颜色的32位值  ,final

    runtimeTypeType:对象的运行时类型,只读, inherited


    方法:

    computeLuminance( ) → double:返回0到1之间的亮度值,0表示最暗,1表示最亮

    toString( ) → String:返回此对象的字符串表示形式 ,override

    withAlpha(int a) → Color: 返回一个新的颜色,但透明度被传入的参数替换,参数的值区间在0到255

    withBlue(int b) → Color:返回一个新的颜色,但blue部分被传入的参数替换,参数的值区间在0到255

    withGreen(int g) → Color: 返回一个新的颜色,但gree部分被传入的参数替换,参数的值区间在0到255

    withOpacity(double opacity) → Color: 返回一个新的颜色,但透明度被传入的参数替换,参数的值区间在0.0到1.0

    withRed(int r) → Color: 返回一个新的颜色,但red部分被传入的参数替换,参数的值区间在0到255

    noSuchMethod(Invocation invocation) → dynamic:访问不存在方法或属性时会调用该方法


    运算子方法:

    operator ==(dynamic other) → bool:覆盖双等号运算 ,override


    静态方法:

    alphaBlend(Color foreground, Color background) → Color:将第一个参数做为透明颜色覆盖到第二个参数上,返回组合成的颜色

    lerp(Color a, Color b, double t) → Color:在两种颜色之间进行线性插值


    原文连接,不许确的地方欢迎指出

    相关文章
    相关标签/搜索