How does a Joy-Con know what color it is?
Solution 1:
When the Joy Con connects to the Nintendo Switch via Bluetooth, it sends some data as a "handshake" between the console and the controller. In this data, the Joy Con itself reports the HEX Color and Side (L/R) of the controller.
This can be confirmed with a program called Joy-Con Toolkit that allows you to see and change the colors reported by the individual controllers just by connecting them via Bluetooth: