I want to make it so that users of my app can search for images by color. I have an image table that saves the colors in hexadecimal as string type.
+----+-----------------+---------------+
| id | name | color |
+----+-----------------+---------------+
| 1 | Orange and Blue | 0c6cab |
| 2 | Black and White | dba27e |
| 3 | Full Colors | 7ba709 |
| 4 | RoG | 970404 |
| 5 | Triangles | 167694 |
+----+-----------------+---------------+
The search must be done with colors similar to the one selected by the user, since if it is done with the exact color, it would be difficult to find many results. Is it possible to do this with hex or do I need to save the colors as another data type?
EDITION 1
By similar colors I mean that when the user searches for a red color, for example, the query returns all the colors with a red hue that are found in the table that do not exactly match the color searched for.
You would have to implement a distance algorithm. For example, let your color table be:
(which is what you were showing)
You can separate each hexadecimal number into its own field:
What I would give you:
Now, you can express those columns with their transformation to decimal:
The "distance" from an RGB color (200,100,120) to each of your colors would be
This would be better encapsulated in a function that takes two hexadecimals, splits them into RGB decimals, and does this calculation, returning the distance.
Once the above is done, you only have to filter the query to those records whose distance from the entered color is less than your tolerance margin.
I think that the way you have it is fine and to make a query looking for similar colors you use the LIKE operator, for example
Let's say the user at #77 and you in your query do this:
the query would return this: