From ed6c036ee3192029e9d0a60e8f9bb2a4ccfb99bf Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 5 Jun 2015 22:50:27 +0000 Subject: [PATCH] Use ints for the calculations rather than u_char, they could end up signed. --- colour.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/colour.c b/colour.c index ac4b72fa..a56ddce9 100644 --- a/colour.c +++ b/colour.c @@ -313,6 +313,7 @@ colour_find_rgb(u_char r, u_char g, u_char b) { struct colour_rgb rgb = { .r = r, .g = g, .b = b }, *found; u_int distance, lowest, colour, i; + int dr, dg, db; found = bsearch(&rgb, colour_to_256, nitems(colour_to_256), sizeof colour_to_256[0], colour_cmp_rgb); @@ -322,11 +323,11 @@ colour_find_rgb(u_char r, u_char g, u_char b) colour = 16; lowest = UINT_MAX; for (i = 0; i < 240; i++) { - r = colour_from_256[i].r - rgb.r; - g = colour_from_256[i].g - rgb.g; - b = colour_from_256[i].b - rgb.b; + dr = (int)colour_from_256[i].r - r; + dg = (int)colour_from_256[i].g - g; + db = (int)colour_from_256[i].b - b; - distance = r * r + g * g + b * b; + distance = dr * dr + dg * dg + db * db; if (distance < lowest) { lowest = distance; colour = 16 + i;