简体   繁体   English

获取图像的当前RGB颜色

[英]Getting current RGB color of Image

I want to change the Alpha color of image(script) component of GameObject . 我想更改GameObjectimage(script)组件的Alpha颜色。

But saving the RGB values of the object. 但是保存对象的RGB值。

I tried the next code: 我尝试了下一个代码:

        targetObject.GetComponent<Image>().color = new Color32(
            (byte)targetObject.GetComponent<Image>().color.r,
            (byte)targetObject.GetComponent<Image>().color.g,
            (byte)targetObject.GetComponent<Image>().color.b, 
            toAlpha);

But (byte)targetObject.GetComponent<Image>().color.r returns 0 what so ever. 但是(byte)targetObject.GetComponent<Image>().color.r返回0如此。

Any ideas? 有任何想法吗?

The struct Color uses channel values in the range [0,1]. 结构Color使用[0,1]范围内的通道值。 So, white is (1,1,1,1) and black (0,0,0,0). 因此,白色为(1,1,1,1),黑色为(0,0,0,0)。 The color propriety returns a Color struct, not a Color32 . color专有性返回Color结构,而不是Color32

When you cast float to byte , the result will be 1 if the channel is at 1, or 0 if is less than 1. Since you're using these values in a Color32 struct (which uses an int per channel in the range [0,255]), if the starting color is full white, you'll have an almost black of (1,1,1,1), if any channel isn't full on (ie <255 using ints), the channel will be full off. 当将floatbyte ,如果通道为1,则结果将为1;如果小于1,则结果为0。因为您正在Color32结构中使用这些值(每个通道的int范围为[0,255] ]),如果起始颜色为全白色,则几乎为(1,1,1,1)黑色,如果任何频道未满(即,使用整数小于255),则该频道将为满关。

Just change new Color32 to new Color and remove the (byte) cast. 只需将new Color32更改为new Color并删除(byte) new Color32转换。

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

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