RGB to Xterm Color Converter
Haskell, 132 bytes



v=0:[95,135..255]
f c=snd$maximum[(-sum(abs&lt;$&gt;zipWith(-)c x),i)|(i,x)&lt;-zip[16..]$[[r,g,b]|r&lt;-v,g&lt;-v,b&lt;-v]++[[g,g,g]|g&lt;-[8,18..238]]]


Try it online!

Takes input as a list of integers [red, green, blue].

Fairly straightforward implementation. First I build a list of the Xterm colors we're using with two list comprehensions concatenated together. The first of which handles colors 16-231 by triple iterating over v which contains the values those colors use. The second one just iterates over the grey values and puts them in all three slots. Then I index it with zip (starting at 16) and make a pair with the manhattan distance (negated) and that index and take the maximum. I used maximum because we're tie-breaking on the largest index and this way saves me one extra -.