Dark colours lost digits in hex
RgbToHex wrote each channel as the shortest hex number, so values below 16 had one digit. The colour 1, 2, 3 gave 123 instead of 010203, and black gave 000 instead of 000000, so Rgb2basic missed it in its lookup table and fell back to an approximate colour.
Every channel gets two digits
Each channel is formatted as %02x, as RgbTo256 in the same file already does. A test covers small channels and checks that RgbToHex(HexToRgb(hex)) returns the same value.