Logo

Programming-Idioms

History of Idiom 154 > diff from v1 to v2

Edit summary for version 2 by programming-idioms.org:
[Go] Comment emphasis

Version 1

2016-10-30, 01:22:01

Version 2

2016-10-30, 01:22:55

Idiom #154 Halfway between two hex color codes

Find color c, the average between colors c1, c2.

c, c1, c2 are strings of hex color codes: 7 chars, beginning with a number sign # .

Idiom #154 Halfway between two hex color codes

Find color c, the average between colors c1, c2.

c, c1, c2 are strings of hex color codes: 7 chars, beginning with a number sign # .

Extra Keywords
hexa css avg mean
Extra Keywords
hexa css avg mean
Imports
import "fmt"
import "strings"
Imports
import "fmt"
import "strings"
Code
r1, _ := strconv.ParseInt(c1[1:3], 16, 0)
r2, _ := strconv.ParseInt(c2[1:3], 16, 0)
r := (r1 + r2) / 2

g1, _ := strconv.ParseInt(c1[3:5], 16, 0)
g2, _ := strconv.ParseInt(c2[3:5], 16, 0)
g := (g1 + g2) / 2

b1, _ := strconv.ParseInt(c1[5:7], 16, 0)
b2, _ := strconv.ParseInt(c2[5:7], 16, 0)
b := (b1 + b2) / 2

c := fmt.Sprintf("#%02X%02X%02X", r, g, b)
Code
r1, _ := strconv.ParseInt(c1[1:3], 16, 0)
r2, _ := strconv.ParseInt(c2[1:3], 16, 0)
r := (r1 + r2) / 2

g1, _ := strconv.ParseInt(c1[3:5], 16, 0)
g2, _ := strconv.ParseInt(c2[3:5], 16, 0)
g := (g1 + g2) / 2

b1, _ := strconv.ParseInt(c1[5:7], 16, 0)
b2, _ := strconv.ParseInt(c2[5:7], 16, 0)
b := (b1 + b2) / 2

c := fmt.Sprintf("#%02X%02X%02X", r, g, b)
Comments bubble
For concision this assumes that input validity has already been_ checked, so we omit some returned errors.
Comments bubble
For concision this assumes that input validity has already been checked, so we omit some returned errors.
Doc URL
http://stackoverflow.com/a/14482509/871134
Doc URL
http://stackoverflow.com/a/14482509/871134
Demo URL
https://play.golang.org/p/Q9kFLdxF0B
Demo URL
https://play.golang.org/p/Q9kFLdxF0B