Skip to content

Commit cd14f24

Browse files
committed
Improve color [0,1]->[0,255] mapping
Resolves #359
1 parent 7f410b6 commit cd14f24

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,12 +1046,6 @@
10461046
inline double ffmin(double a, double b) { return a <= b ? a : b; }
10471047
inline double ffmax(double a, double b) { return a >= b ? a : b; }
10481048

1049-
inline double clamp(double x, double min, double max) {
1050-
if (x < min) return min;
1051-
if (x > max) return max;
1052-
return x;
1053-
}
1054-
10551049
// Common Headers
10561050

10571051
#include "common/ray.h"
@@ -1259,9 +1253,9 @@
12591253
auto b = scale * e[2];
12601254

12611255
// Write the translated [0,255] value of each color component.
1262-
out << static_cast<int>(256.0 * clamp(r, 0.0, 0.999999)) << ' '
1263-
<< static_cast<int>(256.0 * clamp(g, 0.0, 0.999999)) << ' '
1264-
<< static_cast<int>(256.0 * clamp(b, 0.0, 0.999999)) << '\n';
1256+
out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << ' '
1257+
<< static_cast<int>(256 * clamp(g, 0.0, 0.999)) << ' '
1258+
<< static_cast<int>(256 * clamp(b, 0.0, 0.999)) << '\n';
12651259
}
12661260
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12671261
[Listing [write-color-clamped]: <kbd>[vec3.h]</kbd> The write_color() function]
@@ -1495,9 +1489,9 @@
14951489
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
14961490

14971491
// Write the translated [0,255] value of each color component.
1498-
out << static_cast<int>(256.0 * clamp(r, 0.0, 0.999999)) << ' '
1499-
<< static_cast<int>(256.0 * clamp(g, 0.0, 0.999999)) << ' '
1500-
<< static_cast<int>(256.0 * clamp(b, 0.0, 0.999999)) << '\n';
1492+
out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << ' '
1493+
<< static_cast<int>(256 * clamp(g, 0.0, 0.999)) << ' '
1494+
<< static_cast<int>(256 * clamp(b, 0.0, 0.999)) << '\n';
15011495
}
15021496
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15031497
[Listing [write-color-gamma]: <kbd>[vec3.h]</kbd> write_color(), with gamma correction]

books/RayTracingTheRestOfYourLife.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,9 +2305,9 @@
23052305
auto b = sqrt(scale * e[2]);
23062306

23072307
// Write the translated [0,255] value of each color component.
2308-
out << static_cast<int>(255.999 * clamp(r, 0.0, 1.0)) << ' '
2309-
<< static_cast<int>(255.999 * clamp(g, 0.0, 1.0)) << ' '
2310-
<< static_cast<int>(255.999 * clamp(b, 0.0, 1.0)) << '\n';
2308+
out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << ' '
2309+
<< static_cast<int>(256 * clamp(g, 0.0, 0.999)) << ' '
2310+
<< static_cast<int>(256 * clamp(b, 0.0, 0.999)) << '\n';
23112311
}
23122312
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23132313
[Listing [write-color-nan]: <kbd>[common/vec3.h]</kbd> NaN-tolerant write_color function]

src/common/vec3.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ class vec3 {
6868
auto b = sqrt(scale * e[2]);
6969

7070
// Write the translated [0,255] value of each color component.
71-
out << static_cast<int>(255.999 * clamp(r, 0.0, 1.0)) << ' '
72-
<< static_cast<int>(255.999 * clamp(g, 0.0, 1.0)) << ' '
73-
<< static_cast<int>(255.999 * clamp(b, 0.0, 1.0)) << '\n';
71+
out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << ' '
72+
<< static_cast<int>(256 * clamp(g, 0.0, 0.999)) << ' '
73+
<< static_cast<int>(256 * clamp(b, 0.0, 0.999)) << '\n';
7474
}
7575

7676
inline static vec3 random() {

0 commit comments

Comments
 (0)