aboutsummaryrefslogtreecommitdiff
path: root/color.go
blob: dbcb47afa495d2e3cf76414d53641b9f2b15bbdf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

package main

type Color struct {
	r, g, b float64
}

func (color Color) ToPixel() Pixel {
	return Pixel{int(color.r * 255), int(color.g * 255), int(color.b * 255)}
}

func LinearBlend(color1 Color, color2 Color, param float64) Color {
	return Color{
		param*color1.r + (1.0-param)*color2.r,
		param*color1.g + (1.0-param)*color2.g,
		param*color1.b + (1.0-param)*color2.b,
	}
}

func RayColor(ray Ray, hitable Hitable) Color {
	hit := hitable.Hit(ray)
	if hit.time > 0 {
		return Color{1.0, 0.0, 0.0}
	}
	param := 0.5 * (ray.direction.Unit().y + 1)
	return LinearBlend(Color{0.5, 0.7, 1.0}, Color{1.0, 1.0, 1.0}, param)
}