//Method to find the approximate color public async Task <KnownColor> GetClosestColor() { //Create an object to store the raw color data RgbData rgbData = await getRgbData(); //Create a variable to store the closest color. Black by default. KnownColor closestColor = colorList[7]; //Create a variable to store the minimum Euclidean distance between 2 colors double minDiff = double.MaxValue; //For every known color, check the Euclidean distance and store the minimum distance foreach (var color in colorList) { Color colorValue = color.ColorValue; double diff = Math.Pow((colorValue.R - rgbData.Red), 2) + Math.Pow((colorValue.G - rgbData.Green), 2) + Math.Pow((colorValue.B - rgbData.Blue), 2); diff = (int)Math.Sqrt(diff); if (diff < minDiff) { minDiff = diff; closestColor = color; } } //Write the approximate color to the debug console Debug.WriteLine("Approximate color: " + closestColor.ColorName + " - " + closestColor.ColorValue.ToString()); //Return the approximate color return(closestColor); }
//Method to read the RGB data public async Task <RgbData> getRgbData() { //Create an object to store the raw color data RgbData rgbData = new RgbData(); //First get the raw color data ColorData colorData = await getRawData(); //Check if clear data is received if (colorData.Clear > 0) { //Find the RGB values from the raw data using the clear data as reference rgbData.Red = (colorData.Red * 255 / colorData.Clear); rgbData.Blue = (colorData.Blue * 255 / colorData.Clear); rgbData.Green = (colorData.Green * 255 / colorData.Clear); } //Write the RGB values to the debug console Debug.WriteLine("RGB Data - red: {0}, green: {1}, blue: {2}", rgbData.Red, rgbData.Green, rgbData.Blue); //Return the data return(rgbData); }