Thursday, September 19, 2024 9:13:19 PM
> settings

Customize


Authenticate

> MainWindow.xaml.cs
/*
    Bryan
    A3ColorConverter
 */

using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ColorConverter_dotNet
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // Remember these so we know which one to use
        string lastUsedOption = "";

        /// <summary>
        /// Main
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Clear entries
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Clear_Click(object sender, RoutedEventArgs e)
        {
            tb_Hex.Text = "";
            tb_ArmA.Text = "";
            tb_RGB.Text = "";
        }

        /// <summary>
        /// Convert the last modified entry or the different entry
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Convert_Click(object sender, RoutedEventArgs e)
        {
            // Make sure we have something to convert
            if (!tb_ArmA.Text.Equals("") || !tb_Hex.Text.Equals("") || !tb_RGB.Text.Equals(""))
            {
                string[] prefixSuffix = new string[] { "{", "}" };

                // IsChecked returns a nullable, convert to a bool
                if (rb_Array.IsChecked ?? false)
                {
                    prefixSuffix[0] = "[";
                    prefixSuffix[1] = "]";
                }

                // Finally convert the colors and apply them to the text boxes
                switch (lastUsedOption)
                {
                    case "hex":
                        // get the RGB
                        byte[] rgb = ConvertToRGB(tb_Hex.Text);
                        // Format the arma return
                        tb_ArmA.Text = $"{prefixSuffix[0]}{ConvertToArmA(rgb)},1{prefixSuffix[1]}";
                        tb_RGB.Text = $"{rgb[0]},{rgb[1]},{rgb[2]}";
                        break;
                    case "rgb":
                        tb_Hex.Text = $"#{ConvertToHex(ExtractRGB(tb_RGB.Text))}";
                        tb_ArmA.Text = $"{prefixSuffix[0]}{ConvertToArmA(ExtractRGB(tb_RGB.Text))},1{prefixSuffix[1]}";
                        break;
                    case "arma":
                        // Format the arma color
                        string inputColor = tb_ArmA.Text;

                        // Remove the starting brace or bracket
                        if (inputColor.StartsWith("[") || inputColor.StartsWith("{"))
                        {
                            inputColor = inputColor.Substring(1);
                        }

                        // Remove the trailing brace or bracket
                        if (inputColor.EndsWith("]") || inputColor.EndsWith("}"))
                        {
                            inputColor = inputColor.Substring(0, inputColor.Length - 1);
                        }

                        // Convert to a string array
                        string[] rgbArray = inputColor.Split(',');

                        // get us that rgb
                        rgb = ConvertToRGB(new double[]
                        {
                            Double.Parse(rgbArray[0]),
                            Double.Parse(rgbArray[1]),
                            Double.Parse(rgbArray[2])
                        });

                        tb_Hex.Text = $"#{ConvertToHex(rgb)}";
                        tb_RGB.Text = $"{rgb[0]},{rgb[1]},{rgb[2]}";
                        break;
                }
            }
        }


        /// <summary>
        /// Update the color box on text change
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tb_Hex_TextChanged(object sender, TextChangedEventArgs e)
        {
            // Only fire if the hex length is 7
            if (tb_Hex.Text.Length == 7)
            {
                if (IsHex(tb_Hex.Text))
                {
                    byte[] colorRGB = ConvertToRGB(tb_Hex.Text);

                    // Set some colors
                    box_HexColor.Fill = new SolidColorBrush(Color.FromRgb(colorRGB[0], colorRGB[1], colorRGB[2]));
                    tb_Hex.Foreground = new SolidColorBrush(Color.FromRgb(151,151,151));

                    // Remember this
                    lastUsedOption = "hex";
                }
                else
                {
                    box_HexColor.Fill = Brushes.Transparent;
                    tb_Hex.Foreground = Brushes.Red;
                }
            }
            else
            {
                box_HexColor.Fill = Brushes.Transparent;
                tb_Hex.Foreground = Brushes.Red;
            }
        }

        /// <summary>
        /// Update the color box on text change
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tb_RGB_TextChanged(object sender, TextChangedEventArgs e)
        {
            Regex rgx = new Regex(@"^[0-9]+,\s?[0-9]+,\s?[0-9]+");

            // Make sure it's formated correctly
            if (rgx.IsMatch(tb_RGB.Text))
            {
                // Get the rgb from it
                byte[] rgb = ExtractRGB(tb_RGB.Text);

                // Create the color and set it
                box_RGBColor.Fill = new SolidColorBrush(Color.FromRgb(rgb[0], rgb[1], rgb[2]));
                tb_RGB.Foreground = new SolidColorBrush(Color.FromRgb(151, 151, 151));

                // Remember what we used last
                lastUsedOption = "rgb";
            }
            else
            {
                box_RGBColor.Fill = Brushes.Transparent;
                tb_RGB.Foreground = Brushes.Red;
            }
        }

        /// <summary>
        /// Update the color box on text change
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tb_ArmA_TextChanged(object sender, TextChangedEventArgs e)
        {
            // Match [1,1,1,1] or {1,1,1,1} or 1,1,1,1
            Regex rgx = new Regex(@"^[{\[]?[0-1]{1}(?:\.\d+)?,\s?[0-1]{1}(?:\.\d+)?,\s?[0-1]{1}(?:\.\d+)?,\s?[0-1]{1}(?:\.\d+)?[}\]]?$");

            // Make sure it's formated correctly
            if (rgx.IsMatch(tb_ArmA.Text))
            {
                string inputColor = tb_ArmA.Text;

                // Remove the starting brace or bracket
                if (inputColor.StartsWith("[") || inputColor.StartsWith("{"))
                {
                    inputColor = inputColor.Substring(1);
                }

                // Remove the trailing brace or bracket
                if (inputColor.EndsWith("]") || inputColor.EndsWith("}"))
                {
                    inputColor = inputColor.Substring(0, inputColor.Length - 1);
                }

                // Get the color out of the string
                string[] armaColor = inputColor.Split(',');

                // Convert to rgb
                byte[] rgb = ConvertToRGB(new double[]
                {
                    Double.Parse(armaColor[0]),
                    Double.Parse(armaColor[1]),
                    Double.Parse(armaColor[2])
                });

                // Create the color and set it
                box_ArmAColor.Fill = new SolidColorBrush(Color.FromRgb(rgb[0], rgb[1], rgb[2]));
                tb_ArmA.Foreground = new SolidColorBrush(Color.FromRgb(151, 151, 151));

                // Remember this
                lastUsedOption = "arma";
            }
            else
            {
                box_ArmAColor.Fill = Brushes.Transparent;
                tb_ArmA.Foreground = Brushes.Red;
            }
        }
    }
}
All opinions represented herein are my own
- © 2024 itsthedevman
- build 3c15a1b