fogleman / primitive

Reproducing images with geometric primitives.
https://primitive.lol/
MIT License
12.34k stars 607 forks source link

Limiting the Colour Palette #70

Open robbiebarrat opened 6 years ago

robbiebarrat commented 6 years ago

Is there any way to specify a list of available colours? E.g. if I only wanted it to draw with primary colours; (red, yellow, and blue), could I specify this?

Would it be able to learn to mix the available colours and better approximate an image that uses more colours?

I'm totally open to all solutions; even the extremely hacky - thanks.

fogleman commented 6 years ago

Hacky:

Before returning a color on this line...

https://github.com/fogleman/primitive/blob/master/primitive/core.go#L33

...return a different color instead, perhaps by choosing the nearest from a palette.

For example, you can add this snippet before the return statement:


if r > 128 {
    r = 255
} else {
    r = 0
}
if g > 128 {
    g = 255
} else {
    g = 0
}
if b > 128 {
    b = 255
} else {
    b = 0
}```

...and it does a decent job. (best if you let it choose alpha with `-a 0`)
robbiebarrat commented 6 years ago

woah; that's really clever - thank you!

robbiebarrat commented 6 years ago

Sorry; i'm super new to go, but those changes don't seem to have any affect; here's my code:

        r := clampInt(int(rsum/count)>>8, 0, 255)
        g := clampInt(int(gsum/count)>>8, 0, 255)
        b := clampInt(int(bsum/count)>>8, 0, 255)

        if r > b && r > g {
                r = 255
                g = 0
                b = 0
        } else {
                r = 0
        }
        if g > r && g > b {
                g = 255
                r = 0
                b = 0
        } else {
                g = 0
        }
        if b > r && b > g {
                b = 255
                g = 0
                r = 0
        } else {
                b = 0
        }
        alpha = 50

        myslice := []int{r,g,b,alpha}
        fmt.Printf("%#v\n", myslice)

        return Color{r, g, b, alpha}

i imported fmt at the top and everything; do i have to recompile the program or something for it to change? This is literally the first time i've ever used go...

fogleman commented 6 years ago

Use go run main.go instead of primitive

robbiebarrat commented 6 years ago

I've been doing that - as a test i tried changing that last line to return Color{255, 0, 0, alpha}, in hopes that it would return a red image, but it returned a normal multicoloured image instead...

robbiebarrat commented 6 years ago

??

fogleman commented 6 years ago

Dunno dude, it worked for me.

robbiebarrat commented 6 years ago

Could you please detail what commands you ran after editing the file?

Did you just do vi core.go - do your edits - cd .. and then go run main.go (with arguments ofc)? Nothing else?

fogleman commented 6 years ago

Yeah that's it. Show me what you tried.

robbiebarrat commented 6 years ago

I'm at work right now but once i get home in a few hours I'll post a comment with my whole core.go file and the stuff I've tried.

Thanks x100 for helping me out

kajfasz commented 5 years ago

Hi! I've the same problem as @robbiebarrat - sollution that you add in comment above have no effect, but what I'm trying to do is convert monochromatic, grey-scalled image into also monochromatic picture, but e.g. red-scalled (I'm not sure if such thing even exist in english 😃 )

@robbiebarrat, @fogleman - did you find sollution that worked for you guys?

EDIT: Sorry for comment, but I found after some times what I was doing wrong. All magic is to build application inside the go workspace and run command go install in directory where main.go is.