简体   繁体   English

如何检查Color32数组中的特定颜色?

[英]How do I check for a specific color in a Color32 array?

I can't seem to be checking what RGBA a particular index of a Color32 array has. 我似乎无法检查Color32数组的特定索引具有的RGBA。 I'm using this in Unity to "read" colors and convert them into functions which I need to use for later. 我在Unity中使用它来“读取”颜色并将其转换为以后需要使用的功能。 However, Color32 won't accept '==' for an if statement. 但是,对于if语句,Color32将不接受“ ==”。 And a string won't work either. 而且字符串也不起作用。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class SpawnrateGrid : MonoBehaviour {


public Texture2D sourceTex;
Color32[] pix;
int i = 1;


void Start () {
Color32[] pix = sourceTex.GetPixels32();
UnitySystemConsoleRedirector.Redirect();

foreach (Color32 color in pix) {
    Console.WriteLine("Pixel group {0} = {1}", i, color);
    i++;    
        if (string.Equals("RGBA(239, 239, 239, 255)") ){
        }
        if (i>10){
            break;
        }   
    }
}
}

You can check color with name,RGB,RGBA,Conver string to RGB,etc with all of them with == or != , sample code : 您可以使用==!=来检查name,RGB,RGBA,Conver string to RGB,etc颜色,示例代码为:

Color text_color = Color.Black;
            if (text_color == new Color())
            { }

            if (text_color == Color.Transparent)
            { }

            if (text_color != Color.Black)
            { }

            if (text_color != Color.FromArgb(0, 255, 255, 255))
            { }

            if (text_color != Color.FromName("Red"))
            { }

            if (text_color == Color.FromKnownColor(KnownColor.Blue))
            { }

            if (text_color == ColorTranslator.FromHtml("#FFCC66"))
            { }

            if (text_color.R == 255
                && text_color.G == 255
                && text_color.B == 255
                && text_color.A == 0)
            {

            }

If you can it's better to use from ARGB : ( recommend ) 如果可以的话,最好使用ARGB :( 推荐

if (text_color != Color.FromArgb(0, 255, 255, 255))
{ }

Or 要么

if (text_color.R == 255
                && text_color.G == 255
                && text_color.B == 255)
            {

            }

Because Color32 can be converted into a Color , and Color , can be converted into a Vector4 , you may be able to do this: 由于Color32可以转换为Color ,而Color可以转换为Vector4 ,因此您可以执行以下操作:

Vector4 yourColor = new Vector4( 239f, 239f, 239f, 255f ) / 255f ;

foreach (Color32 color in pix)
{
    Console.WriteLine("Pixel group {0} = {1}", i, color);
    i++;
    if( ((Vector4) (Color) color) == yourColor )
    {
        // Do something
    }
    if( i > 10 )
    {
        break ;
    }
}

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

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