-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ColorPicker.cs
89 lines (74 loc) · 3.01 KB
/
ColorPicker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/// <summary>
/// Author: Lele Feng
/// </summary>
using UnityEngine;
using System.Collections;
public class ColorPicker : MonoBehaviour {
public BoxCollider pickerCollider;
private bool m_grab;
private Camera m_camera;
private Texture2D m_screenRenderTexture;
private static Texture2D m_staticRectTexture;
private static GUIStyle m_staticRectStyle;
private static Vector3 m_pixelPosition = Vector3.zero;
private Color m_pickedColor = Color.white;
void Awake() {
// Get the Camera component
m_camera = GetComponent<Camera>();
if (m_camera == null) {
Debug.LogError("You need to dray this script to a camera!");
return;
}
// Attach a BoxCollider to this camera
// In order to receive mouse events
if (pickerCollider == null) {
pickerCollider = gameObject.AddComponent<BoxCollider>();
// Make sure the collider is in the camera's frustum
pickerCollider.center = Vector3.zero;
pickerCollider.center += m_camera.transform.worldToLocalMatrix.MultiplyVector(m_camera.transform.forward) * (m_camera.nearClipPlane + 0.2f);
pickerCollider.size = new Vector3(Screen.width, Screen.height, 0.1f);
}
}
// Draw the color we picked
public static void GUIDrawRect( Rect position, Color color )
{
if( m_staticRectTexture == null )
{
m_staticRectTexture = new Texture2D(1, 1);
}
if( m_staticRectStyle == null )
{
m_staticRectStyle = new GUIStyle();
}
m_staticRectTexture.SetPixel(0, 0, color);
m_staticRectTexture.Apply();
m_staticRectStyle.normal.background = m_staticRectTexture;
GUI.Box(position, GUIContent.none, m_staticRectStyle);
}
// OnPostRender is called after a camera has finished rendering the scene.
// This message is sent to all scripts attached to the camera.
// Use it to grab the screen
// Note: grabing is a expensive operation
void OnPostRender() {
if (m_grab) {
m_screenRenderTexture = new Texture2D(Screen.width, Screen.height);
m_screenRenderTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
m_screenRenderTexture.Apply();
m_pickedColor = m_screenRenderTexture.GetPixel(Mathf.FloorToInt(m_pixelPosition.x), Mathf.FloorToInt(m_pixelPosition.y));
m_grab = false;
}
}
void OnMouseDown() {
m_grab = true;
// Record the mouse position to pick pixel
m_pixelPosition = Input.mousePosition;
}
void OnGUI() {
GUI.Box(new Rect(0, 0, 120, 200), "Color Picker");
GUIDrawRect(new Rect(20, 30, 80, 80), m_pickedColor);
GUI.Label(new Rect(10, 120, 100, 20), "R: " + System.Math.Round((double)m_pickedColor.r, 4) + "\t(" + Mathf.FloorToInt(m_pickedColor.r * 255)+ ")");
GUI.Label(new Rect(10, 140, 100, 20), "G: " + System.Math.Round((double)m_pickedColor.g, 4) + "\t(" + Mathf.FloorToInt(m_pickedColor.g * 255)+ ")");
GUI.Label(new Rect(10, 160, 100, 20), "B: " + System.Math.Round((double)m_pickedColor.b, 4) + "\t(" + Mathf.FloorToInt(m_pickedColor.b * 255)+ ")");
GUI.Label(new Rect(10, 180, 100, 20), "A: " + System.Math.Round((double)m_pickedColor.a, 4) + "\t(" + Mathf.FloorToInt(m_pickedColor.a * 255)+ ")");
}
}