【问题标题】:Calculate ARGB Integer out of HTML Hex color从 HTML Hex 颜色计算 ARGB 整数
【发布时间】:2023-03-30 09:06:01
【问题描述】:

谁能告诉我如何从 Java 中的 HTML 十六进制颜色中获取这个 ARGB 整数值?

这是一个 8 位数的值,显然可能是负数!?

非常感谢!

MatrixToImageConfig(-10223615,-1)

【问题讨论】:

    标签: java colors argb


    【解决方案1】:
    public static void main(String[] args) {
    
        //same colors
        String hex = "#ffffff00";
        Color color = new Color(255,255,255,0);
    
        //4294967040
        System.out.println(Long.decode(hex));
    
        //16777215
        System.out.println(toRGBA(hex));
    
        //16777215
        System.out.println(color.getRGB());
    
        //-16711681
        System.out.println(toARGB(hex));
    }
    
    public static int toRGBA(String nm) {
        Long intval = Long.decode(nm);
        long i = intval.intValue();
    
    
        int a = (int) ((i >> 24) & 0xFF);
        int r = (int) ((i >> 16) & 0xFF);
        int g = (int) ((i >> 8) & 0xFF);
        int b = (int) (i & 0xFF);
    
        return ((b & 0xFF) << 24) |
                ((g & 0xFF) << 16) |
                ((r & 0xFF) << 8)  |
                ((a & 0xFF) << 0);
    }
    
    public static int toARGB(String nm) {
        Long intval = Long.decode(nm);
        long i = intval.intValue();
    
    
        int a = (int) ((i >> 24) & 0xFF);
        int r = (int) ((i >> 16) & 0xFF);
        int g = (int) ((i >> 8) & 0xFF);
        int b = (int) (i & 0xFF);
    
        return ((a & 0xFF) << 24) |
                ((b & 0xFF) << 16) |
                ((g & 0xFF) << 8)  |
                ((r & 0xFF) << 0);
    }
    

    【讨论】:

    • 如果 neogeoz 只需要一个整数值,将其拆分为 ARGB 分量有什么好处?为什么不只是return Integer.decode(nm);
    • @VGR Integer.decode(nm);将十六进制值解码为十进制值,但这与 rgba 值不同
    • 我很确定它是。试试System.out.printf("%08x%n", Integer.decode(nm)); 看看吧。
    • 我编辑了帖子进行小演示,我发现了我的错误
    • 你的方法做了哪些Long.decode没有做到的事情?
    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 2013-06-14
    • 2014-01-18
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 2014-07-18
    相关资源
    最近更新 更多