Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
All RGB
#1
I recently found the site https://allrgb.com/ and also recently saw the stack overflow code golf for a similar project: https://codegolf.stackexchange.com/quest...all-colors

The basic idea is that you generate an image which contains all 15-bit rgb colors.  That's 32 red values, 32 green values, and 32 blue values, for a total of 32768 colors, equally spaced along the color spectrum, creating an image that's 256x128 in size.  That's at a minimum, using more colors would obviously require a larger image, including the full 0-256 spectrum would produce a 4096x4096 image.

The colors should all be used, and none more than once.


That's all the rules.  From them, you can make incredible things (see the source links for examples).

To kick things off, here's a python version of the reference provided on Stack Overflow, with the output it produces.

   

from PIL import Image, ImageDraw

max_width = 256
max_height = 128
dim = (max_width, max_height)

im = Image.new("RGB", dim)
draw = ImageDraw.Draw(im)

width = max_width
step = 8
colors = [(r, g, b)
          for b in range(0, width, step)
          for g in range(0, width, step)
          for r in range(0, width, step)]


for y in range(max_height):
    for x in range(max_width):
        # pop, so each color is only used once
        color = colors.pop(0)
        pos = x, y
        draw.point(pos, color)

# make sure all colors have been used
assert not colors

del draw
im.save("out.png")
im.close()
Reply
#2
cool stuff!
Reply
#3
I think using HSV will probably provide better results than just rgb, so here's an example of using hsv instead, to find colors similar to the last one used that should be used next.  An interesting thing to try next would be to check all neighbors that have already been determined (x and y, instead of just x).  My guess would be that the output would be more circular instead of rectangular.

   

import colorsys
from PIL import Image, ImageDraw


class Color:
   def __init__(self, r, g, b):
       self.r = r
       self.g = g
       self.b = b
       self.hue, self.sat, self.val = colorsys.rgb_to_hsv(r, g, b)

   def similar(self, other):
       hue = abs(self.hue - other.hue)
       saturation = abs(self.sat - other.sat)
       value = abs(self.val - other.val)

       threshold = 8
       return hue <= threshold and saturation <= threshold and value <= threshold

   def values(self):
       return (self.r, self.g, self.b)


max_width = 256
max_height = 128
dim = (max_width, max_height)

im = Image.new("RGB", dim)
draw = ImageDraw.Draw(im)

width = 256
step = 8
colors = [Color(r, g, b)
          for b in range(0, width, step)
          for g in range(0, width, step)
          for r in range(0, width, step)]

last_row = None
for y in range(max_height):
   last = last_row
   for x in range(max_width):
       color = None
       if last:
           for ndx, col in enumerate(colors):
               if col.similar(last):
                   color = colors.pop(ndx)
                   break
       if not color:
           color = colors.pop()
       last = color
       # if this is the first column, keep track of the starting value,
       # so the next row can be similar
       if x == 0:
           last_row = color
       pos = x, y
       draw.point(pos, color.values())

# make sure all colors have been used
assert not colors

del draw
im.save("out.png")
im.close()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020