如何在java中将rgb颜色转换为int [英] how to convert rgb color to int in java

查看:130
本文介绍了如何在java中将rgb颜色转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Paint.setColor 需要一个整数.但我拥有的是一个 Color 对象.我在 Java 中没有看到 color.getIntValue() ?那我该怎么做呢?我想要的是类似的东西

Paint.setColor is expecting an integer. But what I have is a Color object. I don't see a color.getIntValue() in Java? So how do I do that? What I want is something like

public Something myMethod(Color rgb){
    myPaint.setColor(rgb.getIntValue());
    ...
}

更正:android.graphics.Color; 我认为将 android 作为标签之一就足够了.但显然不是.

Correction: android.graphics.Color; I thought having android as one of the tags would be enough. But apparently not.

推荐答案

首先,android.graphics.Color 是一个仅由静态方法组成的类.你如何以及为什么创建一个新的 android.graphics.Color 对象?(这完全没用,对象本身不存储数据)

First of all, android.graphics.Color is a class thats composed of only static methods. How and why did you create a new android.graphics.Color object? (This is completely useless and the object itself stores no data)

但无论如何...我将假设您使用一些实际存储数据的对象...

But anyways... I'm going to assume your using some object that actually stores data...

一个整数由 4 个字节组成(在 Java 中).查看标准 java Color 对象中的 getRGB() 函数,我们可以看到 java 按照 ARGB(Alpha-Red-Green-Blue)的顺序将每种颜色映射到整数的一个字节.我们可以使用自定义方法复制此行为,如下所示:

A integer is composed of 4 bytes (in java). Looking at the function getRGB() from the standard java Color object we can see java maps each color to one byte of the integer in the order ARGB (Alpha-Red-Green-Blue). We can replicate this behavior with a custom method as follows:

public int getIntFromColor(int Red, int Green, int Blue){
    Red = (Red << 16) & 0x00FF0000; //Shift red 16-bits and mask out other stuff
    Green = (Green << 8) & 0x0000FF00; //Shift Green 8-bits and mask out other stuff
    Blue = Blue & 0x000000FF; //Mask out anything not blue.

    return 0xFF000000 | Red | Green | Blue; //0xFF000000 for 100% Alpha. Bitwise OR everything together.
}

这假设您可以以某种方式检索单独的红色、绿色和蓝色颜色分量,并且您为颜色传入的所有值都是 0-255.

This assumes you can somehow retrieve the individual red, green and blue colour components and that all the values you passed in for the colours are 0-255.

如果您的 RGB 值采用介于 0 和 1 之间的浮点百分比形式,请考虑以下方法:

If your RGB values are in form of a float percentage between 0 and 1 consider the following method:

public int getIntFromColor(float Red, float Green, float Blue){
    int R = Math.round(255 * Red);
    int G = Math.round(255 * Green);
    int B = Math.round(255 * Blue);

    R = (R << 16) & 0x00FF0000;
    G = (G << 8) & 0x0000FF00;
    B = B & 0x000000FF;

    return 0xFF000000 | R | G | B;
}

正如其他人所说,如果您使用标准的 java 对象,只需使用 getRGB();

As others have stated, if you're using a standard java object, just use getRGB();

如果您决定正确使用 android 颜色类,您还可以这样做:

If you decide to use the android color class properly you can also do:

int RGB = android.graphics.Color.argb(255, Red, Green, Blue); //Where Red, Green, Blue are the RGB components. The number 255 is for 100% Alpha

int RGB = android.graphics.Color.rgb(Red, Green, Blue); //Where Red, Green, Blue are the RGB components.

正如其他人所说...(第二个函数假设 100% alpha)

as others have stated... (Second function assumes 100% alpha)

这两种方法基本上与上面创建的第一种方法做同样的事情.

Both methods basically do the same thing as the first method created above.

这篇关于如何在java中将rgb颜色转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆