public override object Read(ES2Reader reader) { VoxelColor data = new VoxelColor(); Read(reader, data); return(data); }
public override void Read(ES2Reader reader, object c) { VoxelColor data = (VoxelColor)c; // Add your reader.Read calls here to read the data into the object. data.color = reader.Read <UnityEngine.Color>(); data.number = reader.Read <System.Int32>(); }
public override void Write(object obj, ES2Writer writer) { VoxelColor data = (VoxelColor)obj; // Add your writer.Write calls here. writer.Write(data.color); writer.Write(data.number); }
/// <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> /// Convert a color object into an Hex string. /// </summary> /// <param name="color"></param> /// <returns></returns> public static String ToHex(VoxelColor color) { return ColorUtility.ToHtmlStringRGBA(color); }
/// <summary> /// Get a color for a line texture (HSL mode). /// </summary> /// <param name="coord">Coord of the color between 0f and 1f</param> /// <param name="outColor">Result color.</param> private void GetColorHSL(float coord, VoxelColor outColor) { switch (this._lockedAttr) { case GUIColorPicker.ColorAttr.Hue: outColor.SetHSL( coord, this._selectedColor.Saturation, this._selectedColor.Luminosity ); break; case GUIColorPicker.ColorAttr.Saturation: outColor.SetHSL( this._selectedColor.Hue, coord, this._selectedColor.Luminosity ); break; case GUIColorPicker.ColorAttr.Luminosity: outColor.SetHSL( this._selectedColor.Hue, this._selectedColor.Saturation, coord ); break; default: throw new ColorPickerMissConfigurationException("Invalid locked value for HSL mode : " + this._lockedAttr); } }
/// <summary> /// Commit Hex string to the model. /// </summary> /// <param name="model"></param> private void CommitHex(VoxelColor model) { this.ParseHex(this._hex, model); this.PullRGB(model); this.PullHSL(model); this.PullAlpha(model); }
/// <summary> /// Update a model from the view. (Refresh the view) /// </summary> /// <param name="model"></param> public void CommitView(VoxelColor model) { if(this._hexDirty) { this.CommitHex(model); } else { this.CommitOther(model); } this.Refresh(); }
/// <summary> /// Refresh HSL fields. /// </summary> /// <param name="model"></param> private void PullHSL(VoxelColor model) { this._hue = ((int)(model.Hue * 360)).ToString(); this._saturation = ((int)(model.Saturation * 100)).ToString(); this._luminosity = ((int)(model.Luminosity * 100)).ToString(); }
/// <summary> /// Refresh hexadecimal field. /// </summary> /// <param name="model"></param> private void PullHex(VoxelColor model) { this._hex = GUIColorPicker.ToHex(model); }
/// <summary> /// Repaint the texture. /// </summary> public void Refresh() { VoxelColor color = new VoxelColor(); for (int i = 0; i < this.width; ++i) { for (int j = 0; j < this.height; ++j) { this.GetColorAt(i, j, color); this._texture.SetPixel(i, j, color); } } this._texture.Apply(); }
/// <summary> /// Reset the entire configuration of the object. /// </summary> /// <param name="lockedAttribute"></param> /// <param name="lockedAttributeValue"></param> public void Configure(GUIColorPicker.ColorAttr lockedAttribute, VoxelColor selectedColor) { this._lockedAttr = lockedAttribute; this._selectedColor.Set(selectedColor); this.Refresh(); }
/// <summary> /// Create a new square color texture. /// </summary> /// <param name="width"></param> /// <param name="height"></param> public LineColorPickerTexture(int width, int height) { this._texture = new Texture2D(width, height); this._lockedAttr = GUIColorPicker.ColorAttr.Hue; this._selectedColor = new VoxelColor(); this.Refresh(); }
/// <summary> /// Get a color for a square texture (HSL 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 GetColorHSL(float x, float y, VoxelColor result) { switch (this._lockedAttr) { case GUIColorPicker.ColorAttr.Hue: result.SetHSL(this._selectedColor.Hue, x, y); break; case GUIColorPicker.ColorAttr.Saturation: result.SetHSL(x, this._selectedColor.Saturation, y); break; case GUIColorPicker.ColorAttr.Luminosity: result.SetHSL(x, y, this._selectedColor.Luminosity); break; default: throw new ColorPickerMissConfigurationException("Invalid locked value for HSL mode : " + this._lockedAttr); } }
/// <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); } }
/// <summary> /// Pick a color from the square picker. /// </summary> /// <param name="x">Coord of the color between 0 and width (exclusive, absciss)</param> /// <param name="y">Coord of the color between 0 and height (exclusive, ordinates)</param> /// <param name="result">Result, if instanciation is not required</param> public void GetColorAt(int x, int y, VoxelColor result) { this.GetColorAt(x / (float)this.width, y / (float)this.height, result); }
/// <summary> /// Update view values from a model. /// </summary> /// <param name="model"></param> public void PullModel(VoxelColor model) { this.PullAlpha(model); this.PullRGB(model); this.PullHSL(model); this.PullHex(model); this.Refresh(); }
/// <summary> /// Get a color from a line texture. /// </summary> /// <param name="coord">Coord of the color between 0f and 1f</param> /// <result></result> public VoxelColor GetColorAt(float coord) { VoxelColor result = new VoxelColor(); this.GetColorAt(coord, result); return result; }
/// <summary> /// Refresh alpha field. /// </summary> /// <param name="model"></param> private void PullAlpha(VoxelColor model) { this._alpha = ((int)(model.A * 255)).ToString(); }
/// <summary> /// Get a color from a line texture. /// </summary> /// <param name="x">Coord of the color between 0 and width (exclusive)</param> /// <param name="y">Coord of the color between 0 and height (exclusive)</param> /// <result></result> public VoxelColor GetColorAt(int x, int y) { VoxelColor result = new VoxelColor(); this.GetColorAt(x, y, result); return result; }
/// <summary> /// Refresh RGB fields. /// </summary> /// <param name="model"></param> private void PullRGB(VoxelColor model) { this._red = ((int)(model.R * 255)).ToString(); this._green = ((int)(model.G * 255)).ToString(); this._blue = ((int)(model.B * 255)).ToString(); }
/// <summary> /// Get a color from a line texture. /// </summary> /// <param name="coord">Coord of the color between 0f and 1f</param> /// <param name="outColor">Result color</param> public void GetColorAt(float coord, VoxelColor outColor) { switch (this._lockedAttr) { case GUIColorPicker.ColorAttr.Red: case GUIColorPicker.ColorAttr.Green: case GUIColorPicker.ColorAttr.Blue: this.GetColorRGB(coord, outColor); break; case GUIColorPicker.ColorAttr.Hue: case GUIColorPicker.ColorAttr.Saturation: case GUIColorPicker.ColorAttr.Luminosity: this.GetColorHSL(coord, outColor); break; default: throw new ColorPickerMissConfigurationException("Unhandled locked attribute : " + this._lockedAttr); } }
/// <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 from a line texture. /// </summary> /// <param name="x">Coord of the color between 0 and width (exclusive)</param> /// <param name="y">Coord of the color between 0 and height (exclusive)</param> /// <param name="outColor">Result color</param> public void GetColorAt(int x, int y, VoxelColor outColor) { if (this._isVertical) { this.GetColorAt(y / (float)this.height, outColor); } else { this.GetColorAt(x / (float)this.width, outColor); } }
/// <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> /// Get a color for a line texture (RGB mode). /// </summary> /// <param name="coord">Coord of the color between 0f and 1f</param> /// <param name="outColor">Result color</param> private void GetColorRGB(float coord, VoxelColor outColor) { switch (this._lockedAttr) { case GUIColorPicker.ColorAttr.Red: outColor.R = coord; outColor.G = this._selectedColor.G; outColor.B = this._selectedColor.B; break; case GUIColorPicker.ColorAttr.Green: outColor.R = this._selectedColor.R; outColor.G = coord; outColor.B = this._selectedColor.B; break; case GUIColorPicker.ColorAttr.Blue: outColor.R = this._selectedColor.R; outColor.G = this._selectedColor.G; outColor.B = coord; break; default: throw new ColorPickerMissConfigurationException("Invalid locked value for RGB mode : " + this._lockedAttr); } }