Your product X need to be a Configurable product.
From this Configurable product you can create or assign Simple products (on Simple product by color available).
Tuto on this subject for Magento 2 : How to create configurable product.
Next, switch your simple products visibility to Not visible individually. And I think you should reindex :
php bin/magento indexer:reindex.
Now only your Configurable products are available in search and layered navigation, but containing all color variant in it. So if you search for black one, your Configurable product is display. And if you search for a red one, it is displayed too. Because your Configurable products contains (at least) a red simple product and a black simple product.
In Magento 1, I didn't implement that sort of things, but there is Configurable products too. For layered navigation display, may be you will have to adapt something to works with M1.
Hope that helps