Good Morning,
i would like to search for a solution for my idea.
As this idea might be interesting for someone else, i thought it would be interesting to share it here.
We are currently in the process of creating new Standard Reports for our Customers that replace the 300+ Reports from Acumatica. To add a bit “color” to them, we often use textboxes or lines with a certain color. We would like to change the color based upon the main color of the logo that gets updated - or upon a user definied field that contains the hex of the color.
Customization:
» Step 1 | User uploads a Image at the Screen CS101500 (Most likely the company logo)
» Step 2 | Code starts to look through the Image and return the most used color.
» x--------| Example: For this Picture it would be “CornflowerBlue”
» Step 3 | Code iterates through a defined set of reports (since they are able to get edited within a text editor, i could see this working quite well) and searches within each report for a Color
» x------- | Example: Code opens Report SO641010 - finds the color “Purple” and replaces it with “Cornflower Blue” and saves the report back to the system. The next Time the logo gets
replaced within CS101500 it would search within Reports for “Purple” and replace that..
Another approach instead of searching for colors would be to give the textboxes and lines all the same Name. For example “Colored”, so that the Code can look for the Name and search within the next lines for the “Color” that he changes.
Any Ideas how we can accomplish this?
I found a Code that already quite flawlessly outputs the color within c# based upon a local file.
Would love to receive some help upon converting the code into a customization project and working on the other steps to make this idea a reality as i think it is a great idea to match reports to our customers without much work by hand.
Sincerly
Jannik Westermann
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System;
namespace Bild
{
class Program
{
static void Main(string ] args)
{
var image = (Bitmap)Image.FromFile(@"C:\temp\colorimage3.png");
var mostUsedColor = GetMostUsedColor(image);
var color = GetNearestColor(mostUsedColor);
Console.WriteLine(color.Name);
Console.ReadKey();
}
private static Color GetNearestColor(Color inputColor)
{
var inputRed = Convert.ToDouble(inputColor.R);
var inputGreen = Convert.ToDouble(inputColor.G);
var inputBlue = Convert.ToDouble(inputColor.B);
var colors = new List<Color>();
foreach (var knownColor in Enum.GetValues(typeof(KnownColor)))
{
var color = Color.FromKnownColor((KnownColor)knownColor);
if (!color.IsSystemColor)
colors.Add(color);
}
var nearestColor = Color.Empty;
var distance = 500.0;
foreach (var color in colors)
{
// Compute Euclidean distance between the two colors
var testRed = Math.Pow(Convert.ToDouble(color.R) - inputRed, 2.0);
var testGreen = Math.Pow(Convert.ToDouble(color.G) - inputGreen, 2.0);
var testBlue = Math.Pow(Convert.ToDouble(color.B) - inputBlue, 2.0);
var tempDistance = Math.Sqrt(testBlue + testGreen + testRed);
if (tempDistance == 0.0)
return color;
if (tempDistance < distance)
{
distance = tempDistance;
nearestColor = color;
}
}
return nearestColor;
}
public static Color GetMostUsedColor(Bitmap bitMap)
{
var colorIncidence = new Dictionary<int, int>();
for (var x = 0; x < bitMap.Size.Width; x++)
for (var y = 0; y < bitMap.Size.Height; y++)
{
var pixelColor = bitMap.GetPixel(x, y).ToArgb();
if (colorIncidence.Keys.Contains(pixelColor))
colorIncidence pixelColor]++;
else
colorIncidence.Add(pixelColor, 1);
}
return Color.FromArgb(colorIncidence.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value).First().Key);
}
}
}