Color difference
The perception of color, of course, is subjective, but between people there is significant agreement. For example, we can all agree that red, green, and blue are very different, and even people with foresight agree that black and white are very different.
RGB
The most common color representation in computer systems is the vector (r, g, b), which offers a simple distance function, for example,

Allows you to set the range for r, g and b to [0, 1] and see how it works:
- Red (1, 0, 0) and red (1, 0, 0) have a distance of 0 , which should be obvious
- Red (1, 0, 0) and yellow (1, 0, 0) have a distance of 1 , which is less than the distance
- Red (1, 0, 0) and blue (0, 0, 1), which sqrt (2) , which is plausible
So far so good. However, the problem is that blue and red have the same distance 1 from black (0, 0, 0), but when viewing the image this does not look like this:

Also yellow (1, 1, 0) and purple (1, 0, 1) have the same distance 1 from white (1, 1, 1), which does not seem to make sense:

HSL and HSV
I think it is safe to assume that similar numbers for HSL and HSV color schemes have the same problems. These color schemes are not intended for color comparison.
CIEDE2000
Fortunately, there are scientists who are already trying to find a good way to compare colors. They came up with some sophisticated methods, the last of which is CIEDE2000

(the full formula described in the article is huge)
This metric takes into account human perception, as the fact that we seem to be unable to distinguish between blue shades of blue. Therefore, I would say that we use this as our color separation function.
Color picker
Naive decision
Some answers suggest the following algorithm
colors = [] for n in range(n): success=False while not success: new_color = random_color() for color in colors: if distance(color, new_color)>far_enough: colors.append(new_color) success = True break
This algorithm has some problems:
The interval between colors is not optimal. If we assume that the colors will look like numbers on a line, the three numbers will be optimally distributed as follows:
| ----- ----- b with |
Packing an extra single number there without moving a, b, and c is clearly worse than rearranging all the colors.
The algorithm does not guarantee completion . What if there is no color that is far enough from the existing colors in the list? The cycle will go on forever