Craig Treptow
New Member
Doing this in Scala, while learning Scala.
Posts: 9
|
Post by Craig Treptow on Dec 28, 2018 20:32:29 GMT
Hi there. I've really been enjoying my time with the book so far, but am now feeling really dumb, or that I missed something crucial in the book.
I'm in the initial Canvas section, where I read:
...and then this test description:
Scenario: Constructing the PPM pixel data Given c ← canvas(5, 3) And c1 ← color(1.5, 0, 0) And c2 ← color(0, 0.5, 0) And c3 ← color(-0.5, 0, 1) When write_pixel(c, 0, 0, c1) And write_pixel(c, 2, 1, c2) And write_pixel(c, 4, 2, c3) And ppm ← canvas_to_ppm(c) Then lines 4-6 of ppm are
""" 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 128 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 """
So somehow, 1.5 and 1 are mapped to 255, 0.5 to 128, and -0.5 to 0. There was no need to do any clamping process on anything but the -0.5, right? Was this clamping algorithm given somewhere? I think that's what I don't understand right now.
Any help is appreciated.
- Craig
|
|
fs
New Member
Posts: 28
|
Post by fs on Dec 28, 2018 21:14:47 GMT
One way to do it is to take each of the floating point colour component values, multiply it by 255, then check if it exceeds the allowable limits. If it's less than 0 make it 0, or if it's greater than 255, make it 255.
|
|
|
Post by ascotti on Dec 28, 2018 21:21:00 GMT
Hi, I think it should be something like this:
int_component = (int) component*255.99 if int_component < 0 then int_component = 0 if int_component > 255 then int_component = 255
or you can also have something like this:
int_component = (int) min( max( component, 0), 1 )*255.99
|
|
Craig Treptow
New Member
Doing this in Scala, while learning Scala.
Posts: 9
|
Post by Craig Treptow on Dec 28, 2018 22:04:09 GMT
Oh, thank you for the ideas! I'll give those a whirl and see what I can come up with.
- Craig
|
|
Craig Treptow
New Member
Doing this in Scala, while learning Scala.
Posts: 9
|
Post by Craig Treptow on Dec 29, 2018 1:47:02 GMT
Thank you again. I got past the hump and produced my first functional PPM file! Attachments:
|
|