1 /* 2 * Copyright (c) 2020 Kevin Townsend (KTOWN) 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #include <errno.h> 8 #include <zsl/colorimetry.h> 9 zsl_clr_norm_spd(struct zsl_clr_spd * spd)10int zsl_clr_norm_spd(struct zsl_clr_spd *spd) 11 { 12 zsl_real_t max = 0.0; 13 14 /* Determine the max value. */ 15 for (size_t i = 0; i < spd->size; i++) { 16 if (max < spd->comps[i].value) { 17 max = spd->comps[i].value; 18 } 19 } 20 21 /* Avoid divide by zero. */ 22 if (max == 0.0) { 23 return -EINVAL; 24 } 25 26 /* Scale values by max. */ 27 for (size_t i = 0; i < spd->size; i++) { 28 spd->comps[i].value /= max; 29 } 30 31 return 0; 32 } 33