debug.library: use quick sort to sort the segments
[AROS.git] / rom / graphics / findcolor.c
blobe0afd176324f2770611a45c5e02c353729e94506
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Find the closest matching color in a colormap
6 Lang: english
7 */
8 #include "graphics_intern.h"
9 #include <graphics/view.h>
11 /*****************************************************************************
13 NAME */
14 #include <clib/graphics_protos.h>
16 AROS_LH5(ULONG, FindColor,
18 /* SYNOPSIS */
19 AROS_LHA(struct ColorMap *, cm , A3),
20 AROS_LHA(ULONG , r , D1),
21 AROS_LHA(ULONG , g , D2),
22 AROS_LHA(ULONG , b , D3),
23 AROS_LHA(ULONG , maxpen, D4),
25 /* LOCATION */
26 struct GfxBase *, GfxBase, 168, Graphics)
28 /* FUNCTION
29 Find the closest matching color in the given colormap.
31 INPUTS
32 cm - colormap structure
33 r - red level (32 bit left justified fraction)
34 g - green level (32 bit left justified fraction)
35 b - blue level (32 bit left justified fraction)
36 maxpen - the maximum entry in the color table to search.
38 RESULT
39 The pen number with the closest match will be returned.
40 No new pens are allocated and therefore the returned pen
41 should not be released via ReleasePen().
43 NOTES
45 EXAMPLE
47 BUGS
49 SEE ALSO
51 INTERNALS
53 HISTORY
55 *****************************************************************************/
57 AROS_LIBFUNC_INIT
59 ULONG retval = 0;
61 if (NULL != cm)
63 ULONG index = 0;
64 ULONG best_distance = (ULONG)-1;
65 ULONG distance;
67 if (-1 == maxpen && NULL != cm->PalExtra)
69 /* pe_SharableColors is not the number of colors but the last color index! */
70 maxpen = cm->PalExtra->pe_SharableColors;
73 if (maxpen >= cm->Count) maxpen = cm->Count - 1;
75 while (index <= maxpen)
77 distance = color_distance(cm,r,g,b,index);
78 if (distance < best_distance)
80 best_distance = distance;
81 retval = index;
83 index++;
87 return retval;
89 AROS_LIBFUNC_EXIT
90 } /* FindColor */