Auto color selection PIL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xiaolei

    Auto color selection PIL

    Hi,

    I'm trying to plot some points in an image. Each point has an
    associating type and I'd like to have different colors (preferrably of
    high contrast) for different types. The number of types in the data
    file is unknown a priori. Is there a way to do this at runtime?

    The "solution" I have so far has been to manually create a list of
    10-20 contrasting colors and just go off that list at runtime. This
    works most of the time since the number of types is usually less than
    10. But I'd like a general solution.

    Thank you.

  • Leif K-Brooks

    #2
    Re: Auto color selection PIL

    Xiaolei wrote:
    I'm trying to plot some points in an image. Each point has an
    associating type and I'd like to have different colors (preferrably of
    high contrast) for different types. The number of types in the data
    file is unknown a priori. Is there a way to do this at runtime?
    How about:

    from colorsys import hsv_to_rgb

    def colors(n):
    incr = 1.0 / n
    hue = 0.0
    for i in xrange(n):
    r, g, b = hsv_to_rgb(hue, 1.0, 1.0)
    yield int(r*255), int(g*255), int(b*255)
    hue += incr

    Comment

    • Leif K-Brooks

      #3
      Re: Auto color selection PIL

      Xiaolei wrote:
      I'm trying to plot some points in an image. Each point has an
      associating type and I'd like to have different colors (preferrably of
      high contrast) for different types. The number of types in the data
      file is unknown a priori. Is there a way to do this at runtime?
      If you don't know how many colors are needed even at run time, this code
      might be helpful. But it generates colors that look similar pretty
      quickly, so I wouldn't use it unless you have to. (Anyone know of a
      better algorithm for this?)

      from itertools import count
      from colorsys import hsv_to_rgb

      def hues():
      yield 0.0
      for i in count():
      for j in xrange(2**i):
      yield (1.0 / 2**(i+1)) + ((1.0 / 2**i) * j)

      def colors():
      for hue in hues():
      r, g, b = hsv_to_rgb(hue, 1.0, 1.0)
      yield int(r*255), int(g*255), int(b*255)

      Comment

      • Gabriel Genellina

        #4
        Re: Auto color selection PIL

        At Wednesday 27/9/2006 20:43, Leif K-Brooks wrote:
        I'm trying to plot some points in an image. Each point has an
        associating type and I'd like to have different colors (preferrably of
        high contrast) for different types. The number of types in the data
        file is unknown a priori. Is there a way to do this at runtime?
        >
        >If you don't know how many colors are needed even at run time, this code
        >might be helpful. But it generates colors that look similar pretty
        >quickly, so I wouldn't use it unless you have to. (Anyone know of a
        >better algorithm for this?)
        Try this. It first chooses 0, 1/2, then 1/4, 3/4, then */8...
        It's the best I could make if you don't know the number of colors
        beforehand. If you *do* know how many colors, your previous response is OK.


        from colorsys import hsv_to_rgb

        def hues():
        denom = 1
        yield 0
        while True:
        num = 1
        while num<denom:
        yield float(num)/denom
        num += 2
        denom += denom

        def colors():
        for hue in hues():
        r, g, b = hsv_to_rgb(hue, 1.0, 1.0)
        yield int(r*255), int(g*255), int(b*255)




        Gabriel Genellina
        Softlab SRL





        _______________ _______________ _______________ _____
        Preguntá. Respondé. Descubrí.
        Todo lo que querías saber, y lo que ni imaginabas,
        está en Yahoo! Respuestas (Beta).
        ¡Probalo ya!


        Comment

        • Leif K-Brooks

          #5
          Re: Auto color selection PIL

          Gabriel Genellina wrote:
          Try this. It first chooses 0, 1/2, then 1/4, 3/4, then */8...
          It's the best I could make if you don't know the number of colors
          beforehand. If you *do* know how many colors, your previous response is OK.
          Um, that's the same thing my second suggestion does:
          >>h = hues()
          >>h.next()
          0.0
          >>h.next()
          0.5
          >>h.next()
          0.25
          >>h.next()
          0.75
          >>h.next()
          0.125

          Your implementation is less terse than mine, though. And I suspect it
          runs faster, though I haven't checked that.

          Comment

          • Scott David Daniels

            #6
            Re: Auto color selection PIL

            Leif K-Brooks wrote:
            Gabriel Genellina wrote:
            >Try this. It first chooses 0, 1/2, then 1/4, 3/4, then */8...
            >It's the best I could make if you don't know the number of colors
            >beforehand. If you *do* know how many colors, your previous response
            >is OK.
            I've no better suggestion than either of you, _but_ note that in
            choosing colors for keys it is generally considered better ergonomics
            to vary more than simply the hue if you don't want to penalize the
            colorblind. Consider varying the other two parameters simultaneously
            (perhaps in restricted ranges and varying orders); the contrast may be
            more substantial even to a viewer with full color vision.

            --
            --Scott David Daniels
            scott.daniels@a cm.org

            Comment

            Working...