简体   繁体   English

如何在java中将rgb颜色转换为int

[英]how to convert rgb color to int in java

Paint.setColor is expecting an integer. Paint.setColor需要一个整数。 But what I have is a Color object.但我拥有的是一个Color对象。 I don't see a color.getIntValue() in Java?我在 Java 中没有看到color.getIntValue() So how do I do that?那我该怎么做呢? What I want is something like我想要的是类似的东西

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

Correction: android.graphics.Color;更正: android.graphics.Color; I thought having android as one of the tags would be enough.我认为将android作为标签之一就足够了。 But apparently not.但显然不是。

First of all, android.graphics.Color is a class thats composed of only static methods.首先,android.graphics.Color 是一个仅由静态方法组成的类。 How and why did you create a new android.graphics.Color object?你如何以及为什么创建一个新的 android.graphics.Color 对象? (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...但无论如何......我将假设您使用一些实际存储数据的对象......

A integer is composed of 4 bytes (in java).一个整数由 4 个字节组成(在 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).查看标准 java Color 对象中的 getRGB() 函数,我们可以看到 java 按照 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.
}

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

If your RGB values are in form of a float percentage between 0 and 1 consider the following method:如果您的 RGB 值采用介于 0 和 1 之间的浮点百分比形式,请考虑以下方法:

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;
}

As others have stated, if you're using a standard java object, just use getRGB();正如其他人所说,如果您使用标准的 java 对象,只需使用 getRGB();

If you decide to use the android color class properly you can also do:如果您决定正确使用 android 颜色类,您还可以执行以下操作:

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

or要么

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

as others have stated... (Second function assumes 100% alpha)正如其他人所说......(第二个函数假设 100% alpha)

Both methods basically do the same thing as the first method created above.这两种方法基本上与上面创建的第一种方法做同样的事情。

If you are developing for Android, Color's method for this is rgb(int, int, int)如果你是为 Android 开发,Color 的方法是 rgb(int, int, int)

So you would do something like所以你会做类似的事情

myPaint.setColor(Color.rgb(int, int, int)); 

For retrieving the individual color values you can use the methods for doing so:要检索单个颜色值,您可以使用以下方法:

Color.red(int color) 
Color.blue(int color) 
Color.green(int color) 

Refer to this document for more info有关更多信息,请参阅此文档

Color有一个getRGB()方法,该方法将颜色作为int返回。

你想使用intvalue = Color.parseColor("#" + colorobject);

int color = (A & 0xff) << 24 | int 颜色 = (A & 0xff) << 24 | (R & 0xff) << 16 | (R & 0xff) << 16 | (G & 0xff) << 8 | (G & 0xff) << 8 | (B & 0xff); (B & 0xff);

您可以在 color.xml 中声明一个值,因此您可以通过调用下面的代码来获取整数值。

context.getColor(int resId);
int color =  Color.rgb(red, green, blue);

其中红色、绿色、蓝色是 0 到 255 之间的整数值。

Use getRGB() , it helps ( no complicated programs )使用getRGB() ,它有帮助(没有复杂的程序)

Returns an array of integer pixels in the default RGB color model (TYPE_INT_ARGB) and default sRGB color space, from a portion of the image data.从图像数据的一部分返回默认 RGB 颜色模型 (TYPE_INT_ARGB) 和默认 sRGB 颜色空间中的整数像素数组。

Try this one:试试这个:

Color color = new Color (10,10,10)


myPaint.setColor(color.getRGB());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM