/// <summary> /// Convert a hex string to a color. /// </summary> /// <param name="value"></param> /// <param name="result"></param> /// <returns></returns> public static bool FromHex(String value, VoxelColor result) { Color parsed; if (ColorUtility.TryParseHtmlString(value, out parsed)) { result.Set(parsed); return true; } else { return false; } }
/// <summary> /// Parse a token, an empty value return 0. /// </summary> /// <param name="token"></param> /// <param name="result"></param> /// <returns></returns> private void ParseHex(String token, VoxelColor result) { String toTest = token.Trim(); if (toTest.Equals("")) result.Set(0,0,0,0); else { if (Regex.IsMatch(toTest, "^[0-9ABCDEF]{8}$")) { GUIColorPicker.FromHex(toTest, result); } else if (Regex.IsMatch(toTest, "^[0-9ABCDEF]{4}$")) { GUIColorPicker.FromHex( Regex.Replace(toTest, "([0-9ABCDEF])", "${1}0"), result ); } else { while (toTest.Length < 8) toTest += 0; GUIColorPicker.FromHex(toTest, result); } } }
/// <summary> /// Commit RGB / HSL / A fields to the model. /// </summary> /// <param name="model"></param> private void CommitOther(VoxelColor model) { if (this._rgbDirty) { model.Set( this.Parse(this._red) / 255f, this.Parse(this._green) / 255f, this.Parse(this._blue) / 255f ); this.PullHSL(model); } else if (this._hslDirty) { model.SetHSL( this.Parse(this._hue) / 360f, this.Parse(this._saturation) / 100f, this.Parse(this._luminosity) / 100f ); this.PullRGB(model); } if (this._alphaDirty) { model.A = this.Parse(this._alpha) / 100f; } this.PullHex(model); }
/// <summary> /// Get a color for a square texture (RGB mode). /// </summary> /// <param name="x">Coord of the color between 0f and 1f (absciss)</param> /// <param name="y">Coord of the color between 0f and 1f (ordinates)</param> /// <param name="result">Result, if instanciation is not required</param> private void GetColorRGB(float x, float y, VoxelColor result) { switch (this._lockedAttr) { case GUIColorPicker.ColorAttr.Red: result.Set(this._selectedColor.R, x, y); break; case GUIColorPicker.ColorAttr.Green: result.Set(x, this._selectedColor.G, y); break; case GUIColorPicker.ColorAttr.Blue: result.Set(x, y, this._selectedColor.B); break; default: throw new ColorPickerMissConfigurationException("Invalid locked value for RGB mode : " + this._lockedAttr); } }