Refining the question
I assume that the color is only an example. It's not the best one, since this RGB encoding provides for 16 millions of colors, some of them having a name, but most don't. I'll come back to it
The more general issue is about using some magic code that you use in your database, for example that correspond to a numeric value representing an enum.
Hard-coded or data-driven ?
If you put the magic codes in a lookup table, the more flexible approach is the second one. So put every value that would be uniquely associated with your magic code in your the same table. Proceed exactly as you would do it for any entity in your domain model.
This second approach decouples your code from specific database values. So software is easier to maintain and in the same time your software can be configured very flexibly.
Sometimes you cannot escape a coupling like in approach 1.
Typically, it's when you have some behavior associated with the hard-coded value. Behavior can not always be purely data-driven. A robust practice in this case is to nevertheless use a lookup table, but use the hard-coded value in an "internal code" property. This allows to easily have several codes sharing some behavior, and in the same time would limit and clearly show the very special fields that are tightly linked to the internal code.
And the separation of concerns?
You wonder if you would not mix up different concerns. I think you are misled in thinking that the color name (or the name corresponding to your lookup code) is a UI concern that should not be mixed up with model concerns.
This kind of database design is not against separation of concerns. The fact that the name is used in the UI doesn't make it an UI concern: that name is a property hat belongs to the magic code. If you have codes "h","m" and "s", "hour","minutes","seconds" are not UI concerns. It's first of all a description of the unit codes. The fact that this name is used in the UI is "coincidence".
Back to the color issue
Now that we have discussed the general issue, we can rethink the way to handle the color.
An RGB color is not suitable for a lookup in a table, unless you'd put the 16 million values just for the principle. However, some special values have a name.
So you could consider a strategy like that:
- Lookup if the color code corresponds to a known color name
- If no value if found, construct the color name based on the color components.
The strategy can be fine-tuned according to the needs. For example, you could use a fuzzy naming, i.e. when within a close range of a named color code you'd use that name.
display(color)
even in your first approach? What is the point of testing each possible color individually? Isdisplay
supposed not to only to change the color of some part of the display, but also display a color's name? Please edit your question and clarify.display(color)
you're proposing. But in the end, the color appears to be only an example for the more general problem of having code depending on magic values in the DB.