Random colors for imported categories


(Jay Pfaffman) #1

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.)


(cpradio) #2

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.


(Tomasz Lotocki) #3

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!


(Jay Pfaffman) #4

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:


(Jay Pfaffman) #5

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?


(Gerhard Schlager) #6

Sure, make it so. :+1:


(Jay Pfaffman) #7

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


(Kris) #8

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


(Jay Pfaffman) #9

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


(Gerhard Schlager) #11

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:


(Jay Pfaffman) #12

Nice! That’s brilliant. :clinking_glasses:


(Gerhard Schlager) #13

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