前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊Color中的alpha值

聊聊Color中的alpha值

作者头像
code4it
发布2018-09-17 15:19:17
9340
发布2018-09-17 15:19:17
举报
文章被收录于专栏:码匠的流水账

本文主要介绍下java Color对象中的alpha值。

alpha

java/awt/Color.java

代码语言:javascript
复制
/**
     * Creates an opaque sRGB color with the specified red, green,
     * and blue values in the range (0 - 255).
     * The actual color used in rendering depends
     * on finding the best match given the color space
     * available for a given output device.
     * Alpha is defaulted to 255.
     *
     * @throws IllegalArgumentException if <code>r</code>, <code>g</code>
     *        or <code>b</code> are outside of the range
     *        0 to 255, inclusive
     * @param r the red component
     * @param g the green component
     * @param b the blue component
     * @see #getRed
     * @see #getGreen
     * @see #getBlue
     * @see #getRGB
     */
    public Color(int r, int g, int b) {
        this(r, g, b, 255);
    }

    /**
     * Creates an sRGB color with the specified red, green, blue, and alpha
     * values in the range (0 - 255).
     *
     * @throws IllegalArgumentException if <code>r</code>, <code>g</code>,
     *        <code>b</code> or <code>a</code> are outside of the range
     *        0 to 255, inclusive
     * @param r the red component
     * @param g the green component
     * @param b the blue component
     * @param a the alpha component
     * @see #getRed
     * @see #getGreen
     * @see #getBlue
     * @see #getAlpha
     * @see #getRGB
     */
    @ConstructorProperties({"red", "green", "blue", "alpha"})
    public Color(int r, int g, int b, int a) {
        value = ((a & 0xFF) << 24) |
                ((r & 0xFF) << 16) |
                ((g & 0xFF) << 8)  |
                ((b & 0xFF) << 0);
        testColorValueRange(r,g,b,a);
    }

java里头的color不指定alpha的话,默认其值为255,也就是没有透明度。

opacity

color对象里头的alpha其实是指不透明度,其值范围为0-255,越大越不透明。 其通常对应opacity,这个就是单词语义表达的不透明度,其值范围[0,1.0f],值越大,越不透明。

opacity与alpha的映射

opacity与alpha之间的主要关系列表如下

代码语言:javascript
复制
100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00

这个怎么转义呢,如下

代码语言:javascript
复制
int alpha = Math.round(opacity * 255);

再将int输出为十六进制的表示方式

代码语言:javascript
复制
        String hex = Integer.toHexString(alpha).toUpperCase();
        if (hex.length() == 1){
            hex = "0" + hex;
        }

不足两位往前不零

doc

  • Understanding colors on Android (six characters)
  • Hex transparency in colors
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-10-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • alpha
  • opacity
  • opacity与alpha的映射
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档