Thursday, September 19, 2024 9:11:09 PM
> settings

Customize


Authenticate

> Util.cs
/*
    Bryan
    A3ColorConverter
 */


using System;
using System.Windows;
using System.Windows.Media;

namespace ColorConverter_dotNet
{
    public partial class MainWindow : Window
    {
        /// <summary>
        /// Convert a Hex string to RGB
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        private static byte[] ConvertToRGB(string hex)
        {
            return new byte[]
            {
                Convert.ToByte(hex.Substring(1, 2), 16),
                Convert.ToByte(hex.Substring(3, 2), 16),
                Convert.ToByte(hex.Substring(5, 2), 16)
            };
        }

        /// <summary>
        /// Convert an arma color to RGB
        /// </summary>
        /// <param name="arma"></param>
        /// <returns></returns>
        private static byte[] ConvertToRGB(double[] arma)
        {
            // Return the rgb with a max number of 255
            return new byte[]
            {
                Math.Min((byte)Math.Round(arma[0] * 255), (byte)255),
                Math.Min((byte)Math.Round(arma[1] * 255), (byte)255),
                Math.Min((byte)Math.Round(arma[2] * 255), (byte)255)
            };
        }

        /// <summary>
        /// Convert RGB to hex
        /// </summary>
        /// <param name="rgb"></param>
        /// <returns></returns>
        private static string ConvertToHex(byte[] rgb)
        {
            // Convert to color
            Color color = Color.FromRgb(rgb[0], rgb[1], rgb[2]);

            // return the hex
            return color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2");
        }

        /// <summary>
        /// Convert a RGB color to ArmA and format as a string
        /// </summary>
        /// <param name="rgb"></param>
        /// <returns></returns>
        private static string ConvertToArmA(byte[] rgb)
        {
            double[] armaColor = new double[]
            {
                Math.Min(Math.Round((double)rgb[0] / 255, 3), 1),
                Math.Min(Math.Round((double)rgb[1] / 255, 3), 1),
                Math.Min(Math.Round((double)rgb[2] / 255, 3), 1)
            };
            return $"{armaColor[0]},{armaColor[1]},{armaColor[2]}";
        }


        /// <summary>
        /// Checks to see if the hex string is valid!
        /// </summary>
        /// <param name="chars"></param>
        /// <returns></returns>
        private static bool IsHex(string chars)
        {
            // lower case it
            chars = chars.ToLower();

            // If we have a pound at the beginning, remove it
            if (chars.Length == 7)
                chars = chars.Substring(1);

            // Make sure we have exactly 6
            if (chars.Length != 6) return false;

            // Check the colors
            foreach (var c in chars.ToCharArray())
            {
                if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return false;
            }

            // It was happy, return
            return true;
        }

        /// <summary>
        /// Extracts the RGB color from a RGB string
        /// </summary>
        /// <param name="RGBString"></param>
        /// <returns></returns>
        private byte[] ExtractRGB(string RGBString)
        {
            // Convert separate out the string
            string[] rgbStringArr = RGBString.Split(',');

            // Convert to a RGB byte array
            return new byte[]
            {
                    Byte.Parse(rgbStringArr[0]),
                    Byte.Parse(rgbStringArr[1]),
                    Byte.Parse(rgbStringArr[2])
            };

        }
    }
}
All opinions represented herein are my own
- © 2024 itsthedevman
- build 3c15a1b