NHacker Next
  • new
  • past
  • show
  • ask
  • show
  • jobs
  • submit
Okmain: How to pick an OK main colour of an image (dgroshev.com)
iamcalledrob 53 minutes ago [-]
As a designer, I've built variants of this several times throughout my career.

The author's approach is really good, and he hits on pretty much all the problems that arise from more naive approaches. In particular, using a perceptual colorspace, and how the most representative colour may not be the one that appears the most.

However, image processing makes my neck tingle because there are a lot of footguns. PNG bombs, anyone? I feel like any library needs to either be defensively programmed or explicit in its documentation.

The README says "Finding main colors of a reasonably sized image takes about 100ms" -- that's way too slow. I bet the operation takes a few hundred MB of RAM too.

For anyone that uses this, scale down your images substantially first, or only sample every N pixels. Avoid loading the whole thing into memory if possible, unless this handled serially by a job queue of some sort.

You can operate this kind of algorithm much faster and with less RAM usage on a small thumbnail than you would on a large input image. This makes performance concerns less of an issue. And prevents a whole class of OOM DoS vulnerabilities!

As a defensive step, I'd add something like this https://github.com/iamcalledrob/saferimg/blob/master/asset/p... to your test suite and see what happens.

jaen 22 minutes ago [-]
I really wish people would read the article, the library does exactly this:

> Okmain downsamples the image by a power of two until the total number of pixels is below 250,000.

latexr 49 minutes ago [-]
> I've built variants of this several times throughout my career.

Got any to share? A self-contained command-line tool to get a good palette from an image is something I’d have a use for.

llimllib 44 minutes ago [-]
OKPalette by David Aerne is my favorite tool for this, it chooses points sensibly but then also lets you drag around or change the number of colors you want: https://okpalette.color.pizza/
lemonad 41 minutes ago [-]
This is nice! I looked into this quite a lot some years back when I was trying to summarize IKEA catalogs using color and eventually wrote an R package if you want to look into an alternative to e.g. k-means: https://github.com/lemonad/colorhull (download https://github.com/lemonad/ikea-colors-through-time/blob/mas... for more details on how it works)
seanwilson 36 minutes ago [-]
Nice article! Just curious, do people intuitively understand what a perceptually uniform color space is without much more explanation?

I feel you really need to understand first that typical color spaces like HSL used for color picker UIs have the very counterintuitive flaw that the lightness you see with your eye of a color changes when you only change the saturation or hue sliders. As in, it's very surprising the color spaces most designers use are not perceptually uniform.

latexr 2 hours ago [-]
I’d be interested in trying this out as a command-line tool. It would be useful on its own and the fastest way to evaluate results.
woodrowbarlow 1 hours ago [-]
looks like it's a rust lib with a python wrapper. making a CLI tool should be just a few lines of code.
latexr 8 minutes ago [-]
Yeah, but then I’d have to be working with Python (which I don’t enjoy) and be pulling in dependencies (which I avoid) to have a custom system with moving parts (Python interpreter, library, script) (which I don’t want).

A rust CLI would make a lot of sense here. Single binary.

58 minutes ago [-]
Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact
Rendered at 13:28:12 GMT+0000 (Coordinated Universal Time) with Vercel.