如何转换RGB颜色的java为int [英] how to convert rgb color to int in java

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

问题描述

Paint.setColor 期待一个整数。但我有一个颜色对象。我没有看到一个 color.getIntValue()在Java中?那么,如何做到这一点?我要的是像

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; 我以为有机器人的标签之一就足够了。但显然不是。

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颜色对象,我们可以看到的java函数的getRGB()映射每种颜色的整数中的一个字节的顺序ARGB(阿尔法 - 红 - 绿 - 蓝)。我们可以复制一个自定义的方法,这种行为如下:

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%α)

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

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

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

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

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