public static Color ToRgba(HsvColor hsv, float alpha) { Color rgb = HsvColor.ToRgb(hsv); rgb.a = (__Null)(double)alpha; return(rgb); }
/// <summary> /// Returns the <see cref="Rgb"/> color model equivalent of this <see cref="Hsv"/> color. /// </summary> /// <returns>The <see cref="Rgb"/> equivalent color.</returns> public Rgb ToRgb() { // Instantiating a Color is unfortunately necessary to use existing conversions // Clamping is done internally in the conversion method Color color = HsvColor.ToRgb(H, S, V); return(new Rgb(color)); }
public void Hsv_to_rgb_Is_reversible( [Values(0f / 360f, 60f / 360f, 120f / 360f, 180f / 360f, 240f / 360f, 300f / 360f)] float h) { var originalHsv = new HsvColor(h, 0.5f, 0.5f); var rgb = originalHsv.ToRgb(); var hsv = rgb.ToHsv(); Assert.That(hsv.H, Is.EqualTo(h).Within(0.02)); }
public void ChangeSliderColor() { ImagePack imagePack = this.imgPack[1]; if (imagePack == null || !imagePack.isTex) { return; } int index1 = (int)this.mode % 3; Vector2 size = imagePack.size; int x = (int)size.x; int y = (int)size.y; Color[] colors = new Color[y * x]; switch (this.mode) { case PickerRect.Mode.Hue: case PickerRect.Mode.Saturation: case PickerRect.Mode.Value: float[] rateHsv = this.RateHSV; if (this.mode == PickerRect.Mode.Hue) { rateHsv[1] = 1f; rateHsv[2] = 1f; } for (int index2 = 0; index2 < y; ++index2) { for (int index3 = 0; index3 < x; ++index3) { rateHsv[index1] = Mathf.InverseLerp(0.0f, (float)size.y, (float)index2); colors[index2 * x + index3] = HsvColor.ToRgb(rateHsv[0] * 360f, rateHsv[1], rateHsv[2]); } } break; case PickerRect.Mode.Red: case PickerRect.Mode.Green: case PickerRect.Mode.Blue: float[] rgb = this.RGB; for (int index2 = 0; index2 < y; ++index2) { for (int index3 = 0; index3 < x; ++index3) { rgb[index1] = Mathf.InverseLerp(0.0f, (float)size.y, (float)index2); colors[index2 * x + index3] = new Color(rgb[0], rgb[1], rgb[2]); } } break; } imagePack.SetPixels(colors); }
public override Brush GetChanellLevelBrush(HsvColor c) { LinearGradientBrush br = new LinearGradientBrush(); br.StartPoint = new Point(0, 1); br.EndPoint = new Point(0, 0); Color rgbColor = c.ToRgb(); br.GradientStops.Add(new GradientStop(Color.FromScRgb(1, rgbColor.ScR, rgbColor.ScG, 0), 0)); br.GradientStops.Add(new GradientStop(Color.FromScRgb(1, rgbColor.ScR, rgbColor.ScG, 1), 1)); return(br); }
public void HsvTo() { HsvColor hsv = new HsvColor(208, 0.05882352941176472, 1, 0.5); Assert.AreEqual(0.5, hsv.ToCmy().Alpha, "To CMY"); Assert.AreEqual(0.5, hsv.ToCmyk().Alpha, "To CMYK"); Assert.AreEqual(0.5, hsv.ToHsl().Alpha, "To HSL"); Assert.AreEqual(0.5, hsv.ToHsv().Alpha, "To HSV"); Assert.AreEqual(0.5, hsv.ToRgb().Alpha, "To RGB"); }
private void SetPropertyValueFromHsv(HsvColor hsv) { UI.SuspendControlPainting(this); try { RgbColor rgb = hsv.ToRgb(); SetPropertyValueFromRgb(rgb.Red, rgb.Green, rgb.Blue); if (this.hsvColorWheel.HsvColor != hsv) { this.hsvColorWheel.HsvColor = hsv; } if (this.valueSlider.Value != (hsv.Value * 255) / 100) { this.valueSlider.Value = (hsv.Value * 255) / 100; } if (this.saturationSlider.Value != (hsv.Saturation * 255) / 100) { this.saturationSlider.Value = (hsv.Saturation * 255) / 100; } HsvColor hsvValMin = hsv; hsvValMin.Value = 0; HsvColor hsvValMax = hsv; hsvValMax.Value = 100; this.valueSlider.MinColor = hsvValMin.ToColor(); this.valueSlider.MaxColor = hsvValMax.ToColor(); HsvColor hsvSatMin = hsv; hsvSatMin.Saturation = 0; HsvColor hsvSatMax = hsv; hsvSatMax.Saturation = 100; this.saturationSlider.MinColor = hsvSatMin.ToColor(); this.saturationSlider.MaxColor = hsvSatMax.ToColor(); } finally { UI.ResumeControlPainting(this); if (UI.IsControlPaintingEnabled(this)) { Refresh(); } } }
public override Brush GetColorDetailsBrush(HsvColor c) { int b = c.ToRgb().B; for (int r = 0; r < ByteValues; r++) { for (int g = 0; g < ByteValues; g++) { _pixels[g * ByteValues + r] = ColorUtilities.ColorToBgra32(r, byte.MaxValue - g, b); } } _wb.WritePixels(_sourceRect, _pixels, ByteValues * 4, 0); ImageBrush br = new ImageBrush(_wb); return(br); }
/// <summary> /// Event handler for when an accent color border is pressed. /// This will update the color to the background of the pressed panel. /// </summary> private void AccentBorder_PointerPressed(object?sender, PointerPressedEventArgs e) { Border? border = sender as Border; int accentStep = 0; HsvColor hsvColor = HsvColor; // Get the value component delta try { accentStep = int.Parse(border?.Tag?.ToString() ?? "", CultureInfo.InvariantCulture); } catch { } HsvColor newHsvColor = AccentColorConverter.GetAccent(hsvColor, accentStep); HsvColor oldHsvColor = HsvColor; HsvColor = newHsvColor; OnColorChanged(new ColorChangedEventArgs(oldHsvColor.ToRgb(), newHsvColor.ToRgb())); }
/// <summary> /// Whenever a color is changed via HSV methods, call this and the RGB /// counterparts will be sync'd up. /// </summary> /// <param name="newColor">The HSV color that should be converted to RGB.</param> private void SyncRgbFromHsv(HsvColor newColor) { if (ignore == 0) { ignore++; RgbColor rgbColor = newColor.ToRgb(); Utility.SetNumericUpDownValue(redUpDown, rgbColor.Red); Utility.SetNumericUpDownValue(greenUpDown, rgbColor.Green); Utility.SetNumericUpDownValue(blueUpDown, rgbColor.Blue); string hexText = GetHexNumericUpDownValue(rgbColor.Red, rgbColor.Green, rgbColor.Blue); hexBox.Text = hexText; SetColorGradientValuesRgb(rgbColor.Red, rgbColor.Green, rgbColor.Blue); SetColorGradientMinMaxColorsRgb(rgbColor.Red, rgbColor.Green, rgbColor.Blue); ignore--; } }
private void GenerateColors(Color canvasColor, Color baseColor) { CanvasColor = canvasColor; BaseColor = baseColor; var canvasHsl = HslColor.FromRgb(canvasColor); var canvasHsv = HsvColor.FromRgb(canvasColor); var baseHsv = HsvColor.FromRgb(baseColor); var baseHsl = HslColor.FromRgb(baseColor); TextColor = (canvasHsl.L < 0.4) ? Colors.White : Colors.Black; //GetTextColor(canvasColor); TextInactiveColor = HsvColor.ToRgb(canvasHsv.H, canvasHsv.S, TextColor == Colors.White ? canvasHsv.V + 0.6 : canvasHsv.V - 0.45); TextDisabledColor = HsvColor.ToRgb(canvasHsv.H, canvasHsv.S, TextColor == Colors.White ? canvasHsv.V + 0.3 : canvasHsv.V - 0.15); CanvasDarkColor = HsvColor.ToRgb(canvasHsv.H, canvasHsv.S, TextColor == Colors.White ? canvasHsv.V + 0.5 : canvasHsv.V - 0.5); CanvasLightColor = HsvColor.ToRgb(canvasHsv.H, canvasHsv.S, TextColor == Colors.White ? canvasHsv.V + 0.15 : canvasHsv.V - 0.15); CanvasLighterColor = HsvColor.ToRgb(canvasHsv.H, canvasHsv.S, TextColor == Colors.White ? canvasHsv.V + 0.05 : canvasHsv.V - 0.05); TextLightColor = HsvColor.ToRgb(canvasHsv.H, canvasHsv.S, TextColor == Colors.Black ? canvasHsv.V - 0.6 : canvasHsv.V + 0.5); TextDarkColor = HsvColor.ToRgb(canvasHsv.H, canvasHsv.S, TextColor == Colors.White ? canvasHsv.V - 0.4 : canvasHsv.V + 0.6); // GetTextColor(CanvasDarkColor); BaseTextColor = GetTextColor(BaseColor); // baseHsl.L < 0.65 ? Colors.White : Colors.Black; BaseLightColor = HslColor.ToRgb(baseHsl.H, baseHsl.S, baseHsl.L + 0.3); BaseLightTextColor = GetTextColor(BaseLightColor); BaseDarkColor = HslColor.ToRgb(baseHsl.H, baseHsl.S, baseHsl.L - 0.1); BaseDarkerColor = HslColor.ToRgb(baseHsl.H, baseHsl.S, baseHsl.L - 0.2); ComplementColor = HsvColor.ToRgb(GetComplementHue(baseHsv.H), baseHsv.S, baseHsv.V + (baseHsv.V > 0.5 ? -0.35 : 0)); var compHsl = HslColor.FromRgb(ComplementColor); var compHsv = HsvColor.FromRgb(ComplementColor); ComplementLightColor = HsvColor.ToRgb(compHsv.H, compHsv.S, compHsv.V + 0.2); ComplementDarkColor = HslColor.ToRgb(compHsl.H, compHsl.L - 0.1, compHsl.S); ComplementDarkerColor = HslColor.ToRgb(compHsl.H, compHsl.L - 0.2, compHsl.S); ComplementTextColor = GetTextColor(ComplementColor); ComplementLightTextColor = GetTextColor(ComplementLightColor); CanvasGradientStartColor = HsvColor.ToRgb(canvasHsv.H, canvasHsv.S, canvasHsv.V > 0 ? canvasHsv.V - 0.01 : canvasHsv.V + 0.08); CanvasGradientEndColor = HsvColor.ToRgb(canvasHsv.H, canvasHsv.S, canvasHsv.V > 0 ? canvasHsv.V - 0.08 : canvasHsv.V + 0.2); }
private void ColorWheel_ColorChanged(object sender, EventArgs e) { if (IgnoreChangedEvents) { return; } PushIgnoreChangedEvents(); HsvColor hsvColor = colorWheel.HsvColor; RgbColor rgbColor = hsvColor.ToRgb(); Color color = Color.FromArgb((byte)rgbColor.Red, (byte)rgbColor.Green, (byte)rgbColor.Blue); Utility.SetNumericUpDownValue(hueUpDown, hsvColor.Hue); Utility.SetNumericUpDownValue(saturationUpDown, hsvColor.Saturation); Utility.SetNumericUpDownValue(valueUpDown, hsvColor.Value); Utility.SetNumericUpDownValue(redUpDown, color.R); Utility.SetNumericUpDownValue(greenUpDown, color.G); Utility.SetNumericUpDownValue(blueUpDown, color.B); string hexText = GetHexNumericUpDownValue(color.R, color.G, color.B); hexBox.Text = hexText; SetColorGradientValuesHsv(hsvColor.Hue, hsvColor.Saturation, hsvColor.Value); SetColorGradientMinMaxColorsHsv(hsvColor.Hue, hsvColor.Saturation, hsvColor.Value); SetColorGradientValuesRgb(color.R, color.G, color.B); SetColorGradientMinMaxColorsRgb(color.R, color.G, color.B); Color = color; PopIgnoreChangedEvents(); Update(); }
public void ChangeRectColor() { ImagePack imagePack = this.imgPack[0]; if (imagePack == null || !imagePack.isTex) { return; } int index1 = (int)this.mode % 3; Vector2 size = imagePack.size; int x = (int)size.x; int y = (int)size.y; Color[] colors = new Color[y * x]; switch (this.mode) { case PickerRect.Mode.Hue: case PickerRect.Mode.Saturation: case PickerRect.Mode.Value: float[] rateHsv = this.RateHSV; int[,] numArray1 = new int[3, 3] { { 1, 2, 0 }, { 0, 2, 1 }, { 0, 1, 2 } }; for (int index2 = 0; index2 < y; ++index2) { for (int index3 = 0; index3 < x; ++index3) { rateHsv[numArray1[index1, 0]] = Mathf.InverseLerp(0.0f, (float)size.x, (float)index3); rateHsv[numArray1[index1, 1]] = Mathf.InverseLerp(0.0f, (float)size.y, (float)index2); colors[index2 * x + index3] = HsvColor.ToRgb(360f * rateHsv[0], rateHsv[1], rateHsv[2]); } } break; case PickerRect.Mode.Red: case PickerRect.Mode.Green: case PickerRect.Mode.Blue: float[] rgb = this.RGB; int[,] numArray2 = new int[3, 3] { { 2, 1, 0 }, { 2, 0, 1 }, { 0, 1, 2 } }; for (int index2 = 0; index2 < y; ++index2) { for (int index3 = 0; index3 < x; ++index3) { rgb[numArray2[index1, 0]] = Mathf.InverseLerp(0.0f, (float)size.x, (float)index3); rgb[numArray2[index1, 1]] = Mathf.InverseLerp(0.0f, (float)size.y, (float)index2); colors[index2 * x + index3] = new Color(rgb[0], rgb[1], rgb[2]); } } break; } imagePack.SetPixels(colors); }
private void HsvGradientControl_ValueChanged(object sender, IndexEventArgs e) { if (IgnoreChangedEvents) { return; } int hue; if (sender == hueGradientControl) { hue = (hueGradientControl.Value * 360) / 255; } else { hue = (int)hueUpDown.Value; } int saturation; if (sender == saturationGradientControl) { saturation = (saturationGradientControl.Value * 100) / 255; } else { saturation = (int)saturationUpDown.Value; } int value; if (sender == valueGradientControl) { value = (valueGradientControl.Value * 100) / 255; } else { value = (int)valueUpDown.Value; } HsvColor hsvColor = new HsvColor(hue, saturation, value); colorWheel.HsvColor = hsvColor; RgbColor rgbColor = hsvColor.ToRgb(); Color color = Color.FromArgb((byte)rgbColor.Red, (byte)rgbColor.Green, (byte)rgbColor.Blue); Utility.SetNumericUpDownValue(hueUpDown, hsvColor.Hue); Utility.SetNumericUpDownValue(saturationUpDown, hsvColor.Saturation); Utility.SetNumericUpDownValue(valueUpDown, hsvColor.Value); Utility.SetNumericUpDownValue(redUpDown, rgbColor.Red); Utility.SetNumericUpDownValue(greenUpDown, rgbColor.Green); Utility.SetNumericUpDownValue(blueUpDown, rgbColor.Blue); string hexText = GetHexNumericUpDownValue(rgbColor.Red, rgbColor.Green, rgbColor.Blue); hexBox.Text = hexText; Color = color; Update(); }
private void UpDown_ValueChanged(object sender, System.EventArgs e) { if (IgnoreChangedEvents) { return; } else { PushIgnoreChangedEvents(); if (sender == redUpDown || sender == greenUpDown || sender == blueUpDown) { string hexText = GetHexNumericUpDownValue((int)redUpDown.Value, (int)greenUpDown.Value, (int)blueUpDown.Value); hexBox.Text = hexText; Color rgbColor = Color.FromArgb((byte)redUpDown.Value, (byte)greenUpDown.Value, (byte)blueUpDown.Value); SetColorGradientMinMaxColorsRgb(rgbColor.R, rgbColor.G, rgbColor.B); SetColorGradientValuesRgb(rgbColor.R, rgbColor.G, rgbColor.B); SyncHsvFromRgb(rgbColor); Color = rgbColor; } else if (sender == hexBox) { int hexInt = 0; if (hexBox.Text.Length > 0) { try { hexInt = int.Parse(hexBox.Text, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture); } // Needs to be changed so it reads what the RGB values were last catch (FormatException) { hexInt = 0; hexBox.Text = ""; } catch (OverflowException) { hexInt = 16777215; hexBox.Text = "FFFFFF"; } if (!((hexInt <= 16777215) && (hexInt >= 0))) { hexInt = 16777215; hexBox.Text = "FFFFFF"; } } int newRed = ((hexInt & 0xff0000) >> 16); int newGreen = ((hexInt & 0x00ff00) >> 8); int newBlue = (hexInt & 0x0000ff); Utility.SetNumericUpDownValue(redUpDown, newRed); Utility.SetNumericUpDownValue(greenUpDown, newGreen); Utility.SetNumericUpDownValue(blueUpDown, newBlue); SetColorGradientMinMaxColorsRgb(newRed, newGreen, newBlue); SetColorGradientValuesRgb(newRed, newGreen, newBlue); Color rgbColor = Color.FromArgb((byte)newRed, (byte)newGreen, (byte)newBlue); SyncHsvFromRgb(rgbColor); Color = rgbColor; } else if (sender == hueUpDown || sender == saturationUpDown || sender == valueUpDown) { HsvColor oldHsvColor = colorWheel.HsvColor; HsvColor newHsvColor = new HsvColor((int)hueUpDown.Value, (int)saturationUpDown.Value, (int)valueUpDown.Value); if (oldHsvColor != newHsvColor) { colorWheel.HsvColor = newHsvColor; SetColorGradientValuesHsv(newHsvColor.Hue, newHsvColor.Saturation, newHsvColor.Value); SetColorGradientMinMaxColorsHsv(newHsvColor.Hue, newHsvColor.Saturation, newHsvColor.Value); SyncRgbFromHsv(newHsvColor); RgbColor rgbColor = newHsvColor.ToRgb(); Color = rgbColor.ToColor(); } } PopIgnoreChangedEvents(); } }
public override double GetChanellLevelValue(HsvColor c) { return(ColorUtilities.ScRgbTosRgb(c.ToRgb().ScB)); }
public void Render(Matrix4 model, Matrix4 view, Matrix4 projection) { _framebuffer.Use(); GL.PushMatrix(); var constrainedTemperature = 0; var h = 0.6222222 - constrainedTemperature * 0.05; var s = 0.5 + constrainedTemperature * 0.1; var v = 1; var hsl = HsvColor.ToRgb(h * 360, s, v); GL.ClearColor(hsl); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit); GL.Color3(Color.White); // Set up uniforms _uLightPos.Value = LightPosition; _uMatModel.Value = model; _uMatView.Value = view; _uMatProjection.Value = projection; _uSamples.Value = _framebuffer.Samples; _uTexRandom.Value = 1; _uTexAtlas.Value = 2; GL.ActiveTexture(TextureUnit.Texture1); GL.BindTexture(TextureTarget.Texture2D, _texRandom); GL.ActiveTexture(TextureUnit.Texture2); GL.BindTexture(TextureTarget.Texture2D, _texAtlas.Texture); // Engage shader, render, disengage _shaderModel.Use(_uLightPos, _uMatModel, _uMatView, _uMatProjection, _uSamples, _uTexRandom, _uTexAtlas); foreach (var chunk in Chunks) { chunk?.Draw(); } _shaderScreen.Release(); GL.MatrixMode(MatrixMode.Projection); GL.LoadMatrix(ref projection); GL.MatrixMode(MatrixMode.Modelview); var mat = view * model; GL.LoadMatrix(ref mat); GL.Disable(EnableCap.Lighting); GL.Color3(Color.Red); GL.Begin(PrimitiveType.LineStrip); GL.Vertex3(0, 0, 0); GL.Vertex3(5, 0, 0); GL.End(); GL.Color3(Color.LawnGreen); GL.Begin(PrimitiveType.LineStrip); GL.Vertex3(0, 0, 0); GL.Vertex3(0, 5, 0); GL.End(); GL.Color3(Color.Blue); GL.Begin(PrimitiveType.LineStrip); GL.Vertex3(0, 0, 0); GL.Vertex3(0, 0, 5); GL.End(); GL.Color3(Color.Black); GL.Begin(PrimitiveType.Lines); for (var x = -16; x <= 16; x++) { GL.Vertex3(x * 16, 0, -256); GL.Vertex3(x * 16, 0, 256); } for (var z = -16; z <= 16; z++) { GL.Vertex3(-256, 0, z * 16); GL.Vertex3(256, 0, z * 16); } GL.End(); GL.Enable(EnableCap.Lighting); GL.PopMatrix(); _framebuffer.Release(); // _framebufferUi.Use(); // GL.ClearColor(0, 0, 0, 0); // GL.Clear(ClearBufferMask.ColorBufferBit | // ClearBufferMask.DepthBufferBit | // ClearBufferMask.StencilBufferBit); // // _nvg.BeginFrame(_window.Width, _window.Height, 1); // _nvg.Save(); // // _nvg.FillColor(NanoVg.Rgba(255, 255, 255, 255)); // _nvg.FontFace("sans"); // _nvg.FontSize(18); // _nvg.TextAlign(NvgAlign.Top | NvgAlign.Left); // // _perfGraphFps.RenderGraph(_nvg, 4, 4); // // _nvg.Restore(); // // _ui.Render(this, _nvg); // // _nvg.EndFrame(); // _framebufferUi.Release(); _uWidth.Value = _window.Width; _uHeight.Value = _window.Height; _uTexColor.Value = 0; // _uTexUi.Value = 1; _uSamples.Value = _framebuffer.Samples; // _uSamplesUi.Value = _framebufferUi.Samples; _shaderScreen.Use(_uWidth, _uHeight, _uTexColor, _uSamples); DrawFullscreenQuad(_framebuffer.TextureId); _shaderScreen.Release(); }
public override HsvColor GetColorByColorDetailsMarkerCoord(HsvColor c, Point coord) { Color rgbColor = c.ToRgb(); return(HsvColor.FromRgb(Color.FromScRgb(rgbColor.ScA, ColorUtilities.sRgbToScRgb((float)coord.X), ColorUtilities.sRgbToScRgb((float)coord.Y), rgbColor.ScB))); }
public override void SetColor(HsvColor hsv, PickerRect.Control ctrlType) { this.ColorHSV = hsv; base.ColorRGB = HsvColor.ToRgb(hsv); this.SetColor(ctrlType); }
public override Point GetColorDetailsMarkerCoord(HsvColor c) { Color rgbColor = c.ToRgb(); return(new Point(ColorUtilities.ScRgbTosRgb(rgbColor.ScR), ColorUtilities.ScRgbTosRgb(rgbColor.ScG))); }
public override HsvColor GetColorByChannelLevelValue(HsvColor c, double value) { Color rgbColor = c.ToRgb(); return(HsvColor.FromRgb(Color.FromScRgb(rgbColor.ScA, rgbColor.ScR, rgbColor.ScG, ColorUtilities.sRgbToScRgb((float)value)))); }
public static Color ToRgb(float h, float s, float v) { return(HsvColor.ToRgb(new HsvColor(h, s, v))); }
/// <summary> /// Gets the current color determined by the slider values. /// </summary> private (Color, HsvColor) GetColorFromSliderValues() { HsvColor hsvColor = new HsvColor(); Color rgbColor = new Color(); double sliderPercent = Value / (Maximum - Minimum); var component = ColorComponent; if (ColorModel == ColorModel.Hsva) { var baseHsvColor = HsvColor; switch (component) { case ColorComponent.Alpha: { hsvColor = new HsvColor(sliderPercent, baseHsvColor.H, baseHsvColor.S, baseHsvColor.V); break; } case ColorComponent.Component1: { hsvColor = new HsvColor(baseHsvColor.A, sliderPercent * MaxHue, baseHsvColor.S, baseHsvColor.V); break; } case ColorComponent.Component2: { hsvColor = new HsvColor(baseHsvColor.A, baseHsvColor.H, sliderPercent, baseHsvColor.V); break; } case ColorComponent.Component3: { hsvColor = new HsvColor(baseHsvColor.A, baseHsvColor.H, baseHsvColor.S, sliderPercent); break; } } rgbColor = hsvColor.ToRgb(); } else { var baseRgbColor = Color; byte componentValue = Convert.ToByte(MathUtilities.Clamp(sliderPercent * 255, 0, 255)); switch (component) { case ColorComponent.Alpha: rgbColor = new Color(componentValue, baseRgbColor.R, baseRgbColor.G, baseRgbColor.B); break; case ColorComponent.Component1: rgbColor = new Color(baseRgbColor.A, componentValue, baseRgbColor.G, baseRgbColor.B); break; case ColorComponent.Component2: rgbColor = new Color(baseRgbColor.A, baseRgbColor.R, componentValue, baseRgbColor.B); break; case ColorComponent.Component3: rgbColor = new Color(baseRgbColor.A, baseRgbColor.R, baseRgbColor.G, componentValue); break; } hsvColor = rgbColor.ToHsv(); } if (IsRoundingEnabled) { hsvColor = RoundComponentValues(hsvColor); } return(rgbColor, hsvColor); }
/// <inheritdoc/> protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { if (ignorePropertyChanged) { base.OnPropertyChanged(change); return; } // Always keep the two color properties in sync if (change.Property == ColorProperty) { ignorePropertyChanged = true; HsvColor = Color.ToHsv(); SetColorToSliderValues(); UpdateBackground(); UpdatePseudoClasses(); OnColorChanged(new ColorChangedEventArgs( change.GetOldValue <Color>(), change.GetNewValue <Color>())); ignorePropertyChanged = false; } else if (change.Property == ColorModelProperty) { ignorePropertyChanged = true; SetColorToSliderValues(); UpdateBackground(); UpdatePseudoClasses(); ignorePropertyChanged = false; } else if (change.Property == HsvColorProperty) { ignorePropertyChanged = true; Color = HsvColor.ToRgb(); SetColorToSliderValues(); UpdateBackground(); UpdatePseudoClasses(); OnColorChanged(new ColorChangedEventArgs( change.GetOldValue <HsvColor>().ToRgb(), change.GetNewValue <HsvColor>().ToRgb())); ignorePropertyChanged = false; } else if (change.Property == IsRoundingEnabledProperty) { SetColorToSliderValues(); } else if (change.Property == BoundsProperty) { UpdateBackground(); } else if (change.Property == ValueProperty || change.Property == MinimumProperty || change.Property == MaximumProperty) { ignorePropertyChanged = true; Color oldColor = Color; (var color, var hsvColor) = GetColorFromSliderValues(); if (ColorModel == ColorModel.Hsva) { HsvColor = hsvColor; Color = hsvColor.ToRgb(); } else { Color = color; HsvColor = color.ToHsv(); } UpdatePseudoClasses(); OnColorChanged(new ColorChangedEventArgs(oldColor, Color)); ignorePropertyChanged = false; } base.OnPropertyChanged(change); }
public virtual void SetColor(HsvColor hsv, PickerRect.Control ctrlType) { this.ColorHSV = hsv; this.ColorRGB = HsvColor.ToRgb(hsv); this.SetColor(ctrlType); }
/// <inheritdoc/> protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { if (ignorePropertyChanged) { base.OnPropertyChanged(change); return; } // Always keep the two color properties in sync if (change.Property == ColorProperty) { ignorePropertyChanged = true; HsvColor = Color.ToHsv(); SetColorToHexTextBox(); OnColorChanged(new ColorChangedEventArgs( change.GetOldValue <Color>(), change.GetNewValue <Color>())); ignorePropertyChanged = false; } else if (change.Property == HsvColorProperty) { ignorePropertyChanged = true; Color = HsvColor.ToRgb(); SetColorToHexTextBox(); OnColorChanged(new ColorChangedEventArgs( change.GetOldValue <HsvColor>().ToRgb(), change.GetNewValue <HsvColor>().ToRgb())); ignorePropertyChanged = false; } else if (change.Property == PaletteProperty) { IColorPalette?palette = Palette; // Any custom palette change must be automatically synced with the // bound properties controlling the palette grid if (palette != null) { PaletteColumnCount = palette.ColorCount; List <Color> newPaletteColors = new List <Color>(); for (int shadeIndex = 0; shadeIndex < palette.ShadeCount; shadeIndex++) { for (int colorIndex = 0; colorIndex < palette.ColorCount; colorIndex++) { newPaletteColors.Add(palette.GetColor(colorIndex, shadeIndex)); } } PaletteColors = newPaletteColors; } } else if (change.Property == IsAlphaEnabledProperty) { // Manually coerce the HsvColor value // (Color will be coerced automatically if HsvColor changes) HsvColor = OnCoerceHsvColor(HsvColor); } else if (change.Property == IsColorComponentsVisibleProperty || change.Property == IsColorPaletteVisibleProperty || change.Property == IsColorSpectrumVisibleProperty) { // When the property changed notification is received here the visibility // of individual tab items has not yet been updated through the bindings. // Therefore, the validation is delayed until after bindings update. Dispatcher.UIThread.Post(() => { ValidateSelection(); }, DispatcherPriority.Background); } else if (change.Property == SelectedIndexProperty) { // Again, it is necessary to wait for the SelectedIndex value to // be applied to the TabControl through binding before validation occurs. Dispatcher.UIThread.Post(() => { ValidateSelection(); }, DispatcherPriority.Background); } base.OnPropertyChanged(change); }
/// <summary> /// Generates a new bitmap of the specified size by changing a specific color component. /// This will produce a gradient representing a sweep of all possible values of the color component. /// </summary> /// <param name="width">The pixel width (X, horizontal) of the resulting bitmap.</param> /// <param name="height">The pixel height (Y, vertical) of the resulting bitmap.</param> /// <param name="orientation">The orientation of the resulting bitmap (gradient direction).</param> /// <param name="colorModel">The color model being used: RGBA or HSVA.</param> /// <param name="component">The specific color component to sweep.</param> /// <param name="baseHsvColor">The base HSV color used for components not being changed.</param> /// <param name="isAlphaMaxForced">Fix the alpha component value to maximum during calculation. /// This will remove any alpha/transparency from the other component backgrounds.</param> /// <param name="isSaturationValueMaxForced">Fix the saturation and value components to maximum /// during calculation with the HSVA color model. /// This will ensure colors are always discernible regardless of saturation/value.</param> /// <returns>A new bitmap representing a gradient of color component values.</returns> public static async Task <byte[]> CreateComponentBitmapAsync( int width, int height, Orientation orientation, ColorModel colorModel, ColorComponent component, HsvColor baseHsvColor, bool isAlphaMaxForced, bool isSaturationValueMaxForced) { if (width == 0 || height == 0) { return(new byte[0]); } var bitmap = await Task.Run <byte[]>(() => { int pixelDataIndex = 0; double componentStep; byte[] bgraPixelData; Color baseRgbColor = Colors.White; Color rgbColor; int bgraPixelDataHeight; int bgraPixelDataWidth; // Allocate the buffer // BGRA formatted color components 1 byte each (4 bytes in a pixel) bgraPixelData = new byte[width * height * 4]; bgraPixelDataHeight = height * 4; bgraPixelDataWidth = width * 4; // Maximize alpha component value if (isAlphaMaxForced && component != ColorComponent.Alpha) { baseHsvColor = new HsvColor(1.0, baseHsvColor.H, baseHsvColor.S, baseHsvColor.V); } // Convert HSV to RGB once if (colorModel == ColorModel.Rgba) { baseRgbColor = baseHsvColor.ToRgb(); } // Maximize Saturation and Value components when in HSVA mode if (isSaturationValueMaxForced && colorModel == ColorModel.Hsva && component != ColorComponent.Alpha) { switch (component) { case ColorComponent.Component1: baseHsvColor = new HsvColor(baseHsvColor.A, baseHsvColor.H, 1.0, 1.0); break; case ColorComponent.Component2: baseHsvColor = new HsvColor(baseHsvColor.A, baseHsvColor.H, baseHsvColor.S, 1.0); break; case ColorComponent.Component3: baseHsvColor = new HsvColor(baseHsvColor.A, baseHsvColor.H, 1.0, baseHsvColor.V); break; } } // Create the color component gradient if (orientation == Orientation.Horizontal) { // Determine the numerical increment of the color steps within the component if (colorModel == ColorModel.Hsva) { if (component == ColorComponent.Component1) { componentStep = 360.0 / width; } else { componentStep = 1.0 / width; } } else { componentStep = 255.0 / width; } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (y == 0) { rgbColor = GetColor(x *componentStep); // Get a new color bgraPixelData[pixelDataIndex + 0] = Convert.ToByte(rgbColor.B *rgbColor.A / 255); bgraPixelData[pixelDataIndex + 1] = Convert.ToByte(rgbColor.G *rgbColor.A / 255); bgraPixelData[pixelDataIndex + 2] = Convert.ToByte(rgbColor.R *rgbColor.A / 255); bgraPixelData[pixelDataIndex + 3] = rgbColor.A; } else { // Use the color in the row above // Remember the pixel data is 1 dimensional instead of 2 bgraPixelData[pixelDataIndex + 0] = bgraPixelData[pixelDataIndex + 0 - bgraPixelDataWidth]; bgraPixelData[pixelDataIndex + 1] = bgraPixelData[pixelDataIndex + 1 - bgraPixelDataWidth]; bgraPixelData[pixelDataIndex + 2] = bgraPixelData[pixelDataIndex + 2 - bgraPixelDataWidth]; bgraPixelData[pixelDataIndex + 3] = bgraPixelData[pixelDataIndex + 3 - bgraPixelDataWidth]; } pixelDataIndex += 4; } } } else { // Determine the numerical increment of the color steps within the component if (colorModel == ColorModel.Hsva) { if (component == ColorComponent.Component1) { componentStep = 360.0 / height; } else { componentStep = 1.0 / height; } } else { componentStep = 255.0 / height; } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (x == 0) { // The lowest component value should be at the 'bottom' of the bitmap rgbColor = GetColor((height - 1 - y) * componentStep); // Get a new color bgraPixelData[pixelDataIndex + 0] = Convert.ToByte(rgbColor.B *rgbColor.A / 255); bgraPixelData[pixelDataIndex + 1] = Convert.ToByte(rgbColor.G *rgbColor.A / 255); bgraPixelData[pixelDataIndex + 2] = Convert.ToByte(rgbColor.R *rgbColor.A / 255); bgraPixelData[pixelDataIndex + 3] = rgbColor.A; } else { // Use the color in the column to the left // Remember the pixel data is 1 dimensional instead of 2 bgraPixelData[pixelDataIndex + 0] = bgraPixelData[pixelDataIndex - 4]; bgraPixelData[pixelDataIndex + 1] = bgraPixelData[pixelDataIndex - 3]; bgraPixelData[pixelDataIndex + 2] = bgraPixelData[pixelDataIndex - 2]; bgraPixelData[pixelDataIndex + 3] = bgraPixelData[pixelDataIndex - 1]; } pixelDataIndex += 4; } } } Color GetColor(double componentValue) { Color newRgbColor = Colors.White; switch (component) { case ColorComponent.Component1: { if (colorModel == ColorModel.Hsva) { // Sweep hue newRgbColor = HsvColor.ToRgb( MathUtilities.Clamp(componentValue, 0.0, 360.0), baseHsvColor.S, baseHsvColor.V, baseHsvColor.A); } else { // Sweep red newRgbColor = new Color( baseRgbColor.A, Convert.ToByte(MathUtilities.Clamp(componentValue, 0.0, 255.0)), baseRgbColor.G, baseRgbColor.B); } break; } case ColorComponent.Component2: { if (colorModel == ColorModel.Hsva) { // Sweep saturation newRgbColor = HsvColor.ToRgb( baseHsvColor.H, MathUtilities.Clamp(componentValue, 0.0, 1.0), baseHsvColor.V, baseHsvColor.A); } else { // Sweep green newRgbColor = new Color( baseRgbColor.A, baseRgbColor.R, Convert.ToByte(MathUtilities.Clamp(componentValue, 0.0, 255.0)), baseRgbColor.B); } break; } case ColorComponent.Component3: { if (colorModel == ColorModel.Hsva) { // Sweep value newRgbColor = HsvColor.ToRgb( baseHsvColor.H, baseHsvColor.S, MathUtilities.Clamp(componentValue, 0.0, 1.0), baseHsvColor.A); } else { // Sweep blue newRgbColor = new Color( baseRgbColor.A, baseRgbColor.R, baseRgbColor.G, Convert.ToByte(MathUtilities.Clamp(componentValue, 0.0, 255.0))); } break; } case ColorComponent.Alpha: { if (colorModel == ColorModel.Hsva) { // Sweep alpha newRgbColor = HsvColor.ToRgb( baseHsvColor.H, baseHsvColor.S, baseHsvColor.V, MathUtilities.Clamp(componentValue, 0.0, 1.0)); } else { // Sweep alpha newRgbColor = new Color( Convert.ToByte(MathUtilities.Clamp(componentValue, 0.0, 255.0)), baseRgbColor.R, baseRgbColor.G, baseRgbColor.B); } break; } } return(newRgbColor); } return(bgraPixelData); }); return(bitmap); }
private void OnUpdate(HsvColor newHsvColor) { Color newRgbColor = newHsvColor.ToRgb(); // Update color details if (!_isColorDetailsMarkerDraged) { UpdateColorDetailsMarkerPosition(_colorSelector.GetColorDetailsMarkerCoord(newHsvColor)); colorDetailsBorder.Background = _colorSelector.GetColorDetailsBrush(newHsvColor); } // Update channel level if (!_isChannelLevelValueDraged) { UpdateChannelLevelPosition(_colorSelector.GetChanellLevelValue(newHsvColor)); channelLevelBg.Fill = _colorSelector.GetChanellLevelBrush(newHsvColor); } // Update selected color preview selectedColorPreviewBorder.Background = new SolidColorBrush(newRgbColor); // Update opacity slider if (opacitySlider.Value != newHsvColor.A) { opacitySlider.ValueChanged -= opacitySlider_ValueChanged; opacitySlider.Value = newHsvColor.A; opacitySlider.ValueChanged += opacitySlider_ValueChanged; } // Updating the RGB channels if ((byte)redChannelNumericUpDown.Value != newRgbColor.R) { redChannelNumericUpDown.ValueChanged -= redChannelNumericUpDown_ValueChanged; redChannelNumericUpDown.Value = newRgbColor.R; redChannelNumericUpDown.ValueChanged += redChannelNumericUpDown_ValueChanged; } if ((byte)greenChannelNumericUpDown.Value != newRgbColor.G) { greenChannelNumericUpDown.ValueChanged -= greenChannelNumericUpDown_ValueChanged; greenChannelNumericUpDown.Value = newRgbColor.G; greenChannelNumericUpDown.ValueChanged += greenChannelNumericUpDown_ValueChanged; } if ((byte)blueChannelNumericUpDown.Value != newRgbColor.B) { blueChannelNumericUpDown.ValueChanged -= blueChannelNumericUpDown_ValueChanged; blueChannelNumericUpDown.Value = newRgbColor.B; blueChannelNumericUpDown.ValueChanged += blueChannelNumericUpDown_ValueChanged; } // Updating the HSB channels if ((int)hueNumericUpDown.Value != newHsvColor.IntH) { hueNumericUpDown.ValueChanged -= hueNumericUpDown_ValueChanged; hueNumericUpDown.Value = newHsvColor.IntH; hueNumericUpDown.ValueChanged += hueNumericUpDown_ValueChanged; } if ((int)saturationNumericUpDown.Value != newHsvColor.IntS) { saturationNumericUpDown.ValueChanged -= saturationNumericUpDown_ValueChanged; saturationNumericUpDown.Value = newHsvColor.IntS; saturationNumericUpDown.ValueChanged += saturationNumericUpDown_ValueChanged; } if ((int)brightnessNumericUpDown.Value != newHsvColor.IntV) { brightnessNumericUpDown.ValueChanged -= brightnessNumericUpDown_ValueChanged; brightnessNumericUpDown.Value = newHsvColor.IntV; brightnessNumericUpDown.ValueChanged += brightnessNumericUpDown_ValueChanged; } // Updates hexadecimal color value if (!_isHexValueEdited) { string hexCode = ColorUtilities.GetHexCode(newRgbColor); if (hexValueTextBox.Text != hexCode) { hexValueTextBox.TextChanged -= hexValueTextBox_TextChanged; hexValueTextBox.Text = hexCode; hexValueTextBox.TextChanged += hexValueTextBox_TextChanged; _lastHexValueInput = hexCode; } } }