allrgb

package module
v0.0.0-...-7e8a88b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2014 License: MIT Imports: 5 Imported by: 0

README

Go AllRGB Painting Generator

flower 1

Look mom, I made art!

This Go program generates images that use every color in the RGB color palette exactly once, and tries to do so in as easthetically-pleasing way as possible. It's inspired by AllRGB.com.

It supports different modes.

Tie-dye
go run main/tiedye.go [-file="tiedye"] [-width=512] [-height=256]

tiedye

Flower
go run main/flower.go [-file="flower"] [-width=512] [-height=256]

flower

Pointillist
go run main/pointillist.go [-file="pointillist"] [-width=512] [-height=256]

point

Installation

To install, run:

go get github.com/hermanschaaf/allrgb

Now you can run any of the commands listed above from your $GOHOME/src/github.com/hermanschaaf/allrgb.

The Algorithm

The algorithm is inspired by Joco's blog post. The idea is the following:

  • create a palette of all the available colors
  • start at one point, assign a random color to the point, and add the neighboring points to an available array
  • choose the next color randomly, and find the available spot whose neighborhood best matches the chosen color. Assign the color to this spot, add its neighbors to the available array, and repeat.
The Juicy Details

Creating images pixel-by-pixel is pretty straight-forward in Go:


import (
  "image"
  "image/draw"
  "os"
)

func main() {
  // create the new image file on disk and defer closing it
  toimg, _ := os.Create("colors.png")
  defer toimg.Close()
  
  // create a new drawing
  m := image.NewRGBA(image.Rect(0, 0, width, height))
  
  // choose the point to draw at
  p := image.Point{0, 0}
  
  // create the rectangle we want to draw
  r := image.Rectangle{
  	Min: image.Point{p.X, p.Y},
  	Max: image.Point{p.X + 1, p.Y + 1},
  }
  
  // add one pixel to the drawing
  draw.Draw(m, r, &image.Uniform{pallete[n]}, image.Point{0, 0}, draw.Src)
  
  // save the pixel to png (the rest will be white)
  png.Encode(toimg, m)
}

If we repeat this process in a loop for every color, we have a program that fills a png image with every color :)

Final word

Change the parameters. Try different constraints. Expirement, make your own, and if you get stuck, ask questions! You can find me at @ironzeb, or read my blog at IronZebra.

Personally, I'm going to print one of these onto a canvas very soon!

Documentation

Index

Constants

View Source
const MAXDIFF int = 256*256 + 256*256 + 256*256

Variables

This section is empty.

Functions

func ChoosePoint

func ChoosePoint(c color.RGBA, available []Neighborhood, taken map[image.Point]color.RGBA) (i int)

func ColorDiff

func ColorDiff(c1, c2 color.RGBA) int

func DrawPoint

func DrawPoint(m draw.Image, p image.Point, c color.RGBA)

func GeneratePallete

func GeneratePallete(width, height int) (p []color.RGBA)

generatePallete generates an array of all the colors we can use exactly once

func SetTaken

func SetTaken(width, height, chosenIndex int, p image.Point, c color.RGBA, availableArray *[]Neighborhood, available map[image.Point]bool, taken map[image.Point]color.RGBA)

Types

type ByRand

type ByRand []RandomColor

func (ByRand) Len

func (a ByRand) Len() int

func (ByRand) Less

func (a ByRand) Less(i, j int) bool

func (ByRand) Swap

func (a ByRand) Swap(i, j int)

type Neighborhood

type Neighborhood struct {
	Point image.Point
	Color color.RGBA
}

type RandomColor

type RandomColor struct {
	Rand float64
	C    color.RGBA
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL