1

I must read some HTML with text color definition in rgb (ex rgb(255,0,0) --> red) but I want to search inside the HTML code all similar colors (ex rgb(240, 20, 20) --> similar to red), or another similar color as rgb(223,12,5)… I try do convert it in COLORREF in this mode:

col := RGB(240, 20, 20) --> result = 1316080

col := RGB(255,  0,  0) --> result = 255

but it is not the correct way… Do you have any ideas to have a consecutive value (range) of similar colors?

Thank you

kamentk
  • 513
  • 3
  • 12
ondertol
  • 67
  • 2
  • 10
  • Please don't just ask us to solve the problem for you. Show us how you tried to solve the problem yourself, then show us exactly what the result was, and tell us why you feel it didn't work – jrook Oct 25 '18 at 16:21

1 Answers1

1

I solved a similar task in the following way.

1) I build a structure to contain RGB components for the preferred color space. It can be standard TColor values or maybe you want to use color definitions from Wiki (I used the latter). So I had something like: TArray<TRGB> with all my colors.

2) Analyzed color RGB components for the colors I want to assign to the colors in the color space - obtaining red1, green1, blue1.

3) For each of these colors, I calculated the distance from the colors in the color space as:

dbl_test_red = Sqr(red - red1);
dbl_test_green = Sqr(green - green1);
dbl_test_blue = Sqr(blue - blue1);
distance := dbl_test_blue + dbl_test_green + dbl_test_red;

The minimal distance will correspond to the nearest color. This algorithm doesn't take the alpha-channel into account, but it works well enough for me.

dummzeuch
  • 10,975
  • 4
  • 51
  • 158
Miamy
  • 2,162
  • 3
  • 15
  • 32
  • Why use quadratic rather than linear? And why use floating point rather than integer arithmetic? And rather than adding an answer, it may have been better to refer to one of the many duplicates with excellent answers. – David Heffernan Oct 25 '18 at 17:02
  • Quadratic is needed to avoid negative values - it's a common approach to find distance between two points in multi-dimensional space. Floating point is not needed here, you are right - my solution was written on Java and looks like `double dbl_test_red = Math.pow(red - red1, 2.0);` – Miamy Oct 25 '18 at 18:52
  • You don't need to square to avoid negative values. You can use abs(). Why don't you read the dupes. – David Heffernan Oct 25 '18 at 21:10
  • Miamy, i think your is a good idea!!! but one information… why you don't used the formula of distance between 2 points in 3d space Sqr((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)? – ondertol Oct 26 '18 at 05:03
  • i found this wikipedia article (https://en.wikipedia.org/wiki/Color_difference) that evolve your idea of color distance… do you think is good use the last formula of Eulicean paragraph? – ondertol Oct 26 '18 at 05:50
  • I started with formula you wrote (I think you mean `Sqrt`, not `Sqr`), but thought square root is not needed, because we want to compare distances only. Further, as David said, `abs` can be used instead of `sqr` - this reduces computational payload. Because "This will work in cases when a single color is to be compared to a single color and the need is to simply know whether a distance is greater." - from the same wiki article. About using the last formula - I think it is redundant for the task finding color similarity. But you can try it and compare results, of course. – Miamy Oct 26 '18 at 08:33