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.

6 Likes

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!

3 Likes

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('|')
    colors[rand(colors.count)]
  end

@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?

7 Likes

Sure, make it so. :+1:

4 Likes

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. https://gist.github.com/pfaffman/10d9c170fc7967ef0f2800440f58fa98

5 Likes

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

2 Likes

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

5 Likes

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:

https://github.com/discourse/discourse/commit/eceeef8413d3579c403153af88d84b4d58cfa264

10 Likes

Nice! That’s brilliant. :clinking_glasses:

4 Likes

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