Finding the closest matching number in a text file

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
I'm writing an application that gets the color at the postion where you click on a PictureBox and then searches a comma delimited file for the closest matching color. Each line of the file has the color name, its RGB value, and a number calculated using the distance formula, all separated by commas. My application will perfom the calculation on the color you select from the image, but I don't know how to go through the file to find the three closest matches to that number. Thanks for any help.
 
Use the 'Insert snippet' code editor feature, navigate to 'processing files' and choose 'read delimited text file'. This will give the code to read the file.

Color matching is best done on the HSL color space, which is Hue Saturation and Luminance. Color structure has got methods to get these values from a color. Examine the color picker in Windows Paint, view the custom colors palette and review these values change as you select all colors to give yourself an idea how color concept work and how to compare colors. You should be able to notice how only Hue changes for what the eye perceives as base colors red-yellow-green-blue-red. This is important as Hue is the primary identity of color matching. Next is Saturation, then Luminance.

The easiest lookup is to use an array of all the color values you have read from file plus the color to check, and sort them all according to the above concept. Next your matching algoritm find the color to check and simply get the closest neighbors in sorted array.
 
Thanks. I decided I would try that out. I figured that hue would match it pretty closely. I'll compare all of the items in the text file to the color that I choose from the image and pick the five closest matches by hue, then I'll do the comparison by brighness and then saturation. That should get me a pretty close match, if not a perfect one. I'll also try to do this by RGB value to see the difference. Thanks for your help.
 
Back
Top