- Home /
Find a colour in a Texture2D
I am trying to find a colour that is found on a texture but the method that i have used takes too much time.
string ColourString = colour.ToString ();
for (int i = 0; i < ColourTex2D.height; i++)
{
for (int j = 0; j < ColourTex2D.width; j++)
{
Color TempColour = ColourTex2D.GetPixel(j, i);
if (TempColour.ToString() == ColourString)
{
CrosshairPos = new Vector2(((float)j/ColourTex2D.width) * ColourSelectSize.x, ColourSelectSize.y - ((float)i/ColourTex2D.height) * ColourSelectSize.y);
i=ColourTex2D.height;
j=ColourTex2D.width;
}
}
}
Is there a more effient way of achieving this?
Answer by robertbu · Oct 19, 2014 at 07:52 PM
I'm not sure what you are trying to do with this code. The is likely a better approach than what you are doing to accomplish what you want. But to answer your specific question, you can use Texture2D.GetPixels32() or Textur2D.GetPixels() in start to get the color of all the pixel data. Then you can directly access the pixel data without a GetPixel() call for each position in the texture. GetPixels32() and GetPixels() return a 1D array of the 2D pixel data.
Here is a script that uses GetPixels32(). Attached to a object with texture, clicking on that object will output the color at the cursor position.
using UnityEngine;
using System.Collections;
public class GetColor : MonoBehaviour {
public Texture2D tex;
private Color32[] pixels;
private float width, height;
void Start () {
Texture2D tex = (Texture2D)renderer.material.mainTexture;
pixels = tex.GetPixels32();
width = tex.width;
height = tex.height;
}
void Update () {
RaycastHit hit ;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButton(0) && collider.Raycast(ray, out hit, Mathf.Infinity)) {
Vector3 coord = hit.textureCoord;
int w = (int)(coord.x * width);
int h = (int)(coord.y * height);
Debug.Log (pixels[h * (int)width + w]);
}
}
}
Note that your use of strings for comparisons is also of concern for performance in your code.
Thanks, i used strings because nothing else would work. I tried:
if (Colour1 == Colour2)
//for r, g and b
if((int)(Colour1.r * 1000) == (int)(Colour2.r * 1000))
//for r, g and b
if ($$anonymous$$athf.Approximately(Colour1.r, Colour2.r))
is the problem that they are Color not Color32?
I'm not sure of your code, but if you want to compare with '==', they need to be of the same type. You can cast one from another. Say you had a variable color of type Color and a variable color32 of type Color32, you can do:
if (color == (Color)color32)
Note when comparing colors, unless you are absolutely sure of the values, consider looking at the distance between the two colors and considering them a match if the distance is below a threshold.
float ColorSqrDistance(Color c1, Color c2) {
return ((c2.r - c1.r) * (c2.r - c1.r) + (c2.b - c1.b) * (c2.b - c1.b) + (c2.g - c1.g) * (c2.g - c1.g));
}
Your answer
Follow this Question
Related Questions
Texture2D GetPixel returns the same value everywhere 2 Answers
How can I efficiently determine that certain parts of a texture have a specific color? 1 Answer
Update Texture type to GUI during runtime 0 Answers
Converting a RenderTexture to a Texture2D for use in a shader 2 Answers
Loop trough list of textures and chek mouse position over 0 Answers