Skip to content

Commit 06a85eb

Browse files
authored
Merge pull request #367 from RayTracing/tweak-color-calc
Improve color [0,1]->[0,255] mapping
2 parents b6981a4 + cd14f24 commit 06a85eb

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
@@ -1053,12 +1053,6 @@
10531053
inline double ffmin(double a, double b) { return a <= b ? a : b; }
10541054
inline double ffmax(double a, double b) { return a >= b ? a : b; }
10551055

1056-
inline double clamp(double x, double min, double max) {
1057-
if (x < min) return min;
1058-
if (x > max) return max;
1059-
return x;
1060-
}
1061-
10621056
// Common Headers
10631057

10641058
#include "common/ray.h"
@@ -1266,9 +1260,9 @@
12661260
auto b = scale * e[2];
12671261

12681262
// Write the translated [0,255] value of each color component.
1269-
out << static_cast<int>(256.0 * clamp(r, 0.0, 0.999999)) << ' '
1270-
<< static_cast<int>(256.0 * clamp(g, 0.0, 0.999999)) << ' '
1271-
<< static_cast<int>(256.0 * clamp(b, 0.0, 0.999999)) << '\n';
1263+
out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << ' '
1264+
<< static_cast<int>(256 * clamp(g, 0.0, 0.999)) << ' '
1265+
<< static_cast<int>(256 * clamp(b, 0.0, 0.999)) << '\n';
12721266
}
12731267
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12741268
[Listing [write-color-clamped]: <kbd>[vec3.h]</kbd> The write_color() function]
@@ -1502,9 +1496,9 @@
15021496
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
15031497

15041498
// Write the translated [0,255] value of each color component.
1505-
out << static_cast<int>(256.0 * clamp(r, 0.0, 0.999999)) << ' '
1506-
<< static_cast<int>(256.0 * clamp(g, 0.0, 0.999999)) << ' '
1507-
<< static_cast<int>(256.0 * clamp(b, 0.0, 0.999999)) << '\n';
1499+
out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << ' '
1500+
<< static_cast<int>(256 * clamp(g, 0.0, 0.999)) << ' '
1501+
<< static_cast<int>(256 * clamp(b, 0.0, 0.999)) << '\n';
15081502
}
15091503
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15101504
[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)