private void UpdateColor(double alpha, ColorUpdateReason reason)
        {
            m_currentAlpha = alpha;
            m_currentHex   = GetCurrentHexValue();

            SetColorAndUpdateControls(reason);
        }
        private void UpdateColor(Hsv hsv, ColorUpdateReason reason)
        {
            m_currentHsv = hsv;
            m_currentRgb = ColorConversion.HsvToRgb(hsv);
            m_currentHex = GetCurrentHexValue();

            SetColorAndUpdateControls(reason);
        }
        private void UpdateColor(Rgb rgb, ColorUpdateReason reason)
        {
            m_currentRgb = rgb;
            m_currentHsv = ColorConversion.RgbToHsv(m_currentRgb);
            m_currentHex = GetCurrentHexValue();

            SetColorAndUpdateControls(reason);
        }
        private void SetColorAndUpdateControls(ColorUpdateReason reason)
        {
            m_updatingColor = true;

            Color = ColorConversion.ColorFromRgba(m_currentRgb, m_currentAlpha);
            UpdateColorControls(reason);

            m_updatingColor = false;
        }
        private void UpdateColorControls(ColorUpdateReason reason)
        {
            // If we're updating the controls internally, we don't want to execute any of the controls'
            // event handlers, because that would then update the color, which would update the color controls,
            // and then we'd be in an infinite loop.
            m_updatingControls = true;

            // We pass in the reason why we're updating the color controls because
            // we don't want to re-update any control that was the cause of this update.
            // For example, if a user selected a color on the ColorSpectrum, then we
            // don't want to update the ColorSpectrum's color based on this change.
            if (reason != ColorUpdateReason.ColorSpectrumColorChanged && m_colorSpectrum != null)
            {
                m_colorSpectrum.HsvColor = new Vector4((float)m_currentHsv.H, (float)m_currentHsv.S, (float)m_currentHsv.V, (float)m_currentAlpha);
            }

            if (m_colorPreviewRectangle != null)
            {
                var color = Color;

                m_colorPreviewRectangle.Fill = new SolidColorBrush(color);
            }


            if (reason != ColorUpdateReason.ThirdDimensionSliderChanged && m_thirdDimensionSlider != null)
            {
                UpdateThirdDimensionSlider();
            }

            if (reason != ColorUpdateReason.AlphaSliderChanged && m_alphaSlider != null)
            {
                UpdateAlphaSlider();
            }

            void UpdateTextBoxes()
            {
                if (reason != ColorUpdateReason.RgbTextBoxChanged)
                {
                    if (m_redTextBox != null)
                    {
                        m_redTextBox.Text = ((byte)Math.Round(m_currentRgb.R * 255)).ToString(CultureInfo.InvariantCulture);
                    }

                    if (m_greenTextBox != null)
                    {
                        m_greenTextBox.Text = ((byte)Math.Round(m_currentRgb.G * 255)).ToString(CultureInfo.InvariantCulture);
                    }

                    if (m_blueTextBox != null)
                    {
                        m_blueTextBox.Text = ((byte)Math.Round(m_currentRgb.B * 255)).ToString(CultureInfo.InvariantCulture);
                    }
                }

                if (reason != ColorUpdateReason.HsvTextBoxChanged)
                {
                    if (m_hueTextBox != null)
                    {
                        m_hueTextBox.Text = ((int)Math.Round(m_currentHsv.H)).ToString(CultureInfo.InvariantCulture);
                    }

                    if (m_saturationTextBox != null)
                    {
                        m_saturationTextBox.Text = ((int)Math.Round(m_currentHsv.S * 100)).ToString(CultureInfo.InvariantCulture);
                    }

                    if (m_valueTextBox != null)
                    {
                        m_valueTextBox.Text = ((int)Math.Round(m_currentHsv.V * 100)).ToString(CultureInfo.InvariantCulture);
                    }
                }


                if (reason != ColorUpdateReason.AlphaTextBoxChanged)
                {
                    if (m_alphaTextBox != null)
                    {
                        m_alphaTextBox.Text = ((int)Math.Round(m_currentAlpha * 100)).ToString(CultureInfo.InvariantCulture) + "%";
                    }
                }

                if (reason != ColorUpdateReason.HexTextBoxChanged)
                {
                    if (m_hexTextBox != null)
                    {
                        m_hexTextBox.Text = m_currentHex;
                    }
                }
            };

            var unknownUWPbugPresentInAvalonia = false;

            // TODO review
            if (!unknownUWPbugPresentInAvalonia)
            {
                // A reentrancy bug with setting TextBox.Text was fixed in RS2,
                // so we can just directly set the TextBoxes' Text property there.
                UpdateTextBoxes();
            }
            else if (unknownUWPbugPresentInAvalonia)
            {
                // Otherwise, we need to post this to the dispatcher to avoid that reentrancy bug.
                Dispatcher.UIThread.InvokeAsync(() =>
                {
                    m_updatingControls = true;
                    UpdateTextBoxes();
                    m_updatingControls = false;
                });
            }

            m_updatingControls = false;
        }