primitive icon indicating copy to clipboard operation
primitive copied to clipboard

Limiting the Colour Palette

Open robbiebarrat opened this issue 8 years ago • 11 comments

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.

robbiebarrat avatar Nov 30 '17 01:11 robbiebarrat

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`)

fogleman avatar Nov 30 '17 02:11 fogleman

woah; that's really clever - thank you!

robbiebarrat avatar Nov 30 '17 04:11 robbiebarrat

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...

robbiebarrat avatar Nov 30 '17 05:11 robbiebarrat

Use go run main.go instead of primitive

fogleman avatar Nov 30 '17 12:11 fogleman

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 avatar Nov 30 '17 18:11 robbiebarrat

??

robbiebarrat avatar Dec 04 '17 21:12 robbiebarrat

Dunno dude, it worked for me.

fogleman avatar Dec 04 '17 21:12 fogleman

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?

robbiebarrat avatar Dec 04 '17 22:12 robbiebarrat

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

fogleman avatar Dec 04 '17 22:12 fogleman

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

robbiebarrat avatar Dec 04 '17 22:12 robbiebarrat

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.

kajfasz avatar Jul 08 '18 16:07 kajfasz