aboutsummaryrefslogtreecommitdiff
path: root/lib/vec3.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/vec3.rb')
-rw-r--r--lib/vec3.rb24
1 files changed, 20 insertions, 4 deletions
diff --git a/lib/vec3.rb b/lib/vec3.rb
index e24229a..08cbf69 100644
--- a/lib/vec3.rb
+++ b/lib/vec3.rb
@@ -87,12 +87,28 @@ class Colour < Vec3
def g; @y; end
def b; @z; end
+ def gamma(gma = 2)
+ Colour.new(
+ r > 0 ? r ** (1.0 / gma) : r,
+ g > 0 ? g ** (1.0 / gma) : g,
+ b > 0 ? b ** (1.0 / gma) : b
+ )
+ end
+
+ def clamp(interval)
+ Colour.new(
+ interval.clamp(r),
+ interval.clamp(g),
+ interval.clamp(b)
+ )
+ end
+
def to_ppm
- intensity = Interval.new(0.0, 0.999)
+ g_space = gamma.clamp(Interval.new(0.0, 0.999))
"%3d %3d %3d" % [
- (intensity.clamp(r) * 256).to_i,
- (intensity.clamp(g) * 256).to_i,
- (intensity.clamp(b) * 256).to_i
+ (g_space.r * 256).to_i,
+ (g_space.g * 256).to_i,
+ (g_space.b * 256).to_i
]
end
end