mardi 30 avril 2019

How to randomly select HEX colors in MySQL but avoid a specific color range?

THE BACKGROUND

I am using a MySQL database to store data which I retrieve in data visualization on my website. Two out of ~800 items are of main interest in my visualization and will have a specific color code assigned to them (dark and light orange) inside a bar chart. Filters are used for the visualization, so each time a different set of items could be visible, but the 2 main items should always stick out (if existent after the filter).

DESIRED SOLUTION

I want to achieve both

  • A colorful bar chart
  • easy visual recognition of the two main items by avoiding similar colors in the chart

MY CURRENT SOLUTIONS

One way might be to just create a random set of hex colors, manually (or semi-automatically) change all colors of items that have a similar color code to those of the two main items and store it as a list to be retrieved. And for now, that would be my strategy...

But I would really like to have an automated way which would just create random hex codes, but avoid a specific color range (in my case everything orange).

I know already how to randomly create a hex code:

concat('#',SUBSTRING((lpad(hex(round(rand() * 10000000)),6,0))

Now the next step would be the avoidance of the orange color scheme.

MY QUESTION

Any ideas on how to achieve this inside a SELECT statement?

SELECT
   item,
   concat('#',SUBSTRING((lpad(hex(round(rand() * 10000000)),6,0)) as color --THIS SHOULD CHANGE
   FROM item_list




Aucun commentaire:

Enregistrer un commentaire