/// <summary> /// Move the hue slider to the hue of the input color /// </summary> /// /// <param name="newColor"> /// New target color /// </param> public void MoveToColor(Color newColor) { if (huePanel == null) { Start(); } HSBColor hsbColor = new HSBColor(newColor); float minY = huePanel.transform.position.y - huePanel.GetComponent <RectTransform>().rect.height + (huePanel.GetComponent <RectTransform>().rect.height / 2) + 3; float maxY = huePanel.transform.position.y + (huePanel.GetComponent <RectTransform>().rect.height / 2) - 3; float range = 204; float newY = (-207) + (range * hsbColor.h); gameObject.GetComponent <RectTransform>().anchoredPosition = new Vector2(0, newY); }
/// <summary> /// Update appropriate UI fields after any one of the RGB values are changed /// </summary> public void RGBChanged() { // Get the RGB values float r = float.Parse(redInputField.text); float g = float.Parse(greenInputField.text); float b = float.Parse(blueInputField.text); // Ensure that colors are within the right range (0 to 255) if (r > 255) { redInputField.text = "255"; r = 255; } if (g > 255) { greenInputField.text = "255"; g = 255; } if (b > 255) { blueInputField.text = "255"; b = 255; } // Scale to between 0.0 and 1.0 r /= 255; g /= 255; b /= 255; // Convert the RGB values to a color Color newColorRGB = new Color(r, g, b); currentColor = new HSBColor(newColorRGB); // Update the current color image currentColorImage.color = newColorRGB; // Update the hexInputField hexInputField.text = "#" + ColorToHex(newColorRGB); // Update the position of the Hue slider hueSlider.MoveToColor(newColorRGB); // Update the shader on the HSB swatch hsbSwatch.MoveToColor(new HSBColor(newColorRGB)); }
/// <summary> /// Update the current color based on changes from a different UI component /// </summary> /// /// <param name="hue"> /// New hue value (if less than 0.0, assume hue unchanged) /// </param> /// <param name="saturation"> /// New saturation value (if less than 0.0, assume saturation unchanged) /// </param> /// <param name="brightness"> /// New brightness value (if less than 0.0, assume brightness unchanged) /// </param> public void SetColor(float hue, float saturation, float brightness) { // Updated HSB values float newHue = currentColor.h; float newSaturation = currentColor.s; float newBrightness = currentColor.b; // Update the HUE if (hue >= 0.0f) { newHue = hue; } // Update the saturation if (saturation > 0.0f) { newSaturation = saturation; } // Update the brightness if (brightness > 0.0f) { newBrightness = brightness; } currentColor = new HSBColor(newHue, newSaturation, newBrightness); Color rgbColor = currentColor.ToColor(); // Update the UI components with the new color's values currentColorImage.color = rgbColor; hexInputField.text = "#" + ColorToHex(rgbColor); float r = (float)Math.Truncate(rgbColor.r * 1000.0f) / 1000.0f; float g = (float)Math.Truncate(rgbColor.g * 1000.0f) / 1000.0f; float b = (float)Math.Truncate(rgbColor.b * 1000.0f) / 1000.0f; // Update the RGB Input Fields redInputField.text = ((int)(r * 255)).ToString(); greenInputField.text = ((int)(g * 255)).ToString(); blueInputField.text = ((int)(b * 255)).ToString(); if (saturation < 0.0f || brightness < 0.0f) { // Update the shader on the HSB swatch hsbSwatch.MoveToColor(new HSBColor(rgbColor)); } }
public static HSBColor Lerp(HSBColor a, HSBColor b, float t) { float h, s; //check special case black (color.b==0): interpolate neither hue nor saturation! //check special case grey (color.s==0): don't interpolate hue! if (a.b == 0) { h = b.h; s = b.s; } else if (b.b == 0) { h = a.h; s = a.s; } else { if (a.s == 0) { h = b.h; } else if (b.s == 0) { h = a.h; } else { // works around bug with LerpAngle float angle = Mathf.LerpAngle(a.h * 360f, b.h * 360f, t); while (angle < 0f) { angle += 360f; } while (angle > 360f) { angle -= 360f; } h = angle / 360f; } s = Mathf.Lerp(a.s, b.s, t); } return(new HSBColor(h, s, Mathf.Lerp(a.b, b.b, t), Mathf.Lerp(a.a, b.a, t))); }
/// <summary> /// Store the current color choosing, remembering the previous color /// </summary> public void OK() { previousColor = currentColor; previousColorImage.color = previousColor.ToColor(); for (int i = (recentColorImages.Length - 1); i >= 0; i--) { if (i == 0) { recentColorImages[i].GetComponent <Image>().color = previousColor.ToColor(); } else { Color beforeColor = recentColorImages[i - 1].GetComponent <Image>().color; recentColorImages[i].GetComponent <Image>().color = beforeColor; } } gameObject.SetActive(false); }
/// <summary> /// Revert to the input image's color /// </summary> public void RevertToColor(Image image) { currentColor = new HSBColor(image.color); Color currentColorRGB = currentColor.ToColor(); // Update the current color image currentColorImage.color = currentColorRGB; // Update the RGB Input Fields redInputField.text = ((int)(currentColorRGB.r * 255)).ToString(); greenInputField.text = ((int)(currentColorRGB.g * 255)).ToString(); blueInputField.text = ((int)(currentColorRGB.b * 255)).ToString(); // Update the hexInputField hexInputField.text = "#" + ColorToHex(currentColorRGB); // Update the position of the Hue slider hueSlider.MoveToColor(currentColorRGB); // Update the shader on the HSB swatch hsbSwatch.MoveToColor(new HSBColor(currentColorRGB)); }
public static void Test() { HSBColor color; color = new HSBColor(Color.red); Debug.Log("red: " + color); color = new HSBColor(Color.green); Debug.Log("green: " + color); color = new HSBColor(Color.blue); Debug.Log("blue: " + color); color = new HSBColor(Color.grey); Debug.Log("grey: " + color); color = new HSBColor(Color.white); Debug.Log("white: " + color); color = new HSBColor(new Color(0.4f, 1f, 0.84f, 1f)); Debug.Log("0.4, 1f, 0.84: " + color); Debug.Log("164,82,84 .... 0.643137f, 0.321568f, 0.329411f :" + ToColor(new HSBColor(new Color(0.643137f, 0.321568f, 0.329411f)))); }