Михаил обнови решението на 12.11.2013 09:19 (преди над 4 години)
+package main
+
+import (
+ "fmt"
+ "math"
+)
+
+type Header struct {
+ Format string
+ LineWidth int
+}
+
+type Image struct {
+ header Header
+ data []byte
+ makePixel func(format string, data []byte) Pixel
+}
+
+type Pixel struct {
+ color Color
+}
+
+type Color struct {
+ Red byte
+ Blue byte
+ Green byte
+}
+
+func ParseImage(data []byte, header Header) (image Image) {
+ image = *(new(Image))
+ image.header = header
+ image.data = data
+ image.makePixel = func(format string, data []byte) Pixel {
+ pixel := *(new(Pixel))
+ color := new(Color)
+ alpha := 1.0
+ for index, colorCode := range format {
+ switch colorCode {
+ case 'R':
+ color.Red = data[index]
+ case 'G':
+ color.Green = data[index]
+ case 'B':
+ color.Blue = data[index]
+ case 'A':
+ alpha = float64(data[index]) / 255.0
+
+ default:
+ panic("You are an idiot")
+ }
+ }
+ color.Red = byte(round(float64(color.Red) * alpha))
+ color.Blue = byte(round(float64(color.Blue) * alpha))
+ color.Green = byte(round(float64(color.Green) * alpha))
+ pixel.color = *color
+ return pixel
+ }
+ return
+}
+
+func round(float float64) int64 {
+ floor := math.Floor(float)
+ diff := float - floor
+ if diff < 0.5 {
+ return int64(floor)
+ } else {
+ return int64(floor + 1)
+ }
+}
+
+func (i *Image) InspectPixel(row, column int) (pixel Pixel) {
+ colorComponentsPerPixel := len(i.header.Format)
+ begining := colorComponentsPerPixel*row + column*i.header.LineWidth*colorComponentsPerPixel
+ pixel = i.makePixel(i.header.Format, i.data[begining:])
+ return
+}
+
+func (p *Pixel) Color() (result Color) {
+ return p.color
+}
+
+func (c *Color) String() string {
+ return fmt.Sprintf("Red: %d, Green: %d, Blue: %d", c.Red, c.Green, c.Blue)
+}
ще го напиша по хубаво като си напишете условието