//Method to find the approximate color public async Task <string> 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[0]; //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); Debug.WriteLine("{0} diff={1}", color.colorName, 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.colorName); }
//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 = (int)(colorData.Red * 255 / colorData.Clear); rgbData.Blue = (int)(colorData.Blue * 255 / colorData.Clear); rgbData.Green = (int)(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); }