Random colors for imported categories

A client just asked that an importer create random colors for imported categories.

Does anyone else think that’s a good idea? if so, should I modify create_category in base.rb so that everyone can enjoy?

Should I cycle through the default colors or just make them some function of the category name?

Or should I tell them “no”? (Which was my inclination.)

1 Like

I’m not sure it is a bad idea, but I’d be skeptical of making it truly random. Instead I’d create a safe list of background and foreground colors (so you don’t end up with horrible contrast) and then randomly select a set for each category. Maybe come up with 10-20 combinations and if someone has more categories than that, they will just have to get used to it repeating colors.


I think random from an array of default category colours set will do the trick and is just a nice addition to the whole process.

Thanks Jay!


That’s pretty much what I was thinking, though I think I might rather just pull from the default suggested categories, if only I knew where to look for them. I know enough about Rails to know that they are exactly where they should be, but not enough to know where to look. :slight_smile:

Agreed. Rather than random, this function chooses a random value from the set of category colors in site settings.

  def random_category_color
    colors = SiteSetting.category_colors.split('|')

@gerhard, if I submit this as a PR in base.rb, should it just replace the boring, ugly, default brown, or have some kind of switch to enable the random color and leave brown as the default?


Sure, make it so. :+1:


Someone just asked about this. I’ve not managed to test and submit this, but here’s this if someone wants it sooner rather than later. base.rb · GitHub


This would be great to see implemented if someone has time to test it. I’m tired of the default :poop: color.


I’ve got a handful of importer PRs to submit and will endeavor to get this submitted soon.


Random category colors still looked like :poop: most of the time, so I changed your code snippet a little bit. It now uses the colors in the order defined in site settings. That looks a lot better. :rainbow:


Nice! That’s brilliant. :clinking_glasses:


This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.