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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18022364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:51:25  来源:igfitidea点击:

how to convert rgb color to int in java

javaandroidandroid-layoutcolorspaint

提问by Cote Mounyo

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

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

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

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

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

回答by chrylis -cautiouslyoptimistic-

Colorhas a getRGB()method that returns the color as an int.

Color有一个getRGB()方法将颜色作为int.

回答by lexpfb

Try this one:

试试这个:

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


myPaint.setColor(color.getRGB());

回答by superdiazepam

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 documentfor more info

有关更多信息,请参阅此文档

回答by initramfs

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)

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

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

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

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:

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

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.

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

回答by TangQisen

You may declare a value in color.xml, and thus you can get integer value by calling the code below.

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

context.getColor(int resId);

回答by meow

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 颜色空间中的整数像素数组。

回答by JoppeD

You want to use intvalue = Color.parseColor("#" + colorobject);

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

回答by Amol Jindal

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

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