| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2022 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #include <geogram/image/color.h> | ||
| 41 | |||
| 42 | namespace { | ||
| 43 | using namespace GEO; | ||
| 44 | |||
| 45 | // taken from: | ||
| 46 | // https://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion | ||
| 47 | |||
| 48 | /** | ||
| 49 | * \brief Helper for hsl_to_rgb() | ||
| 50 | */ | ||
| 51 | ✗ | double hue_to_rgb(double p, double q, double t) { | |
| 52 | ✗ | if (t < 0.0) t += 1.0; | |
| 53 | ✗ | if (t > 1.0) t -= 1.0; | |
| 54 | ✗ | if (t < 1.0 / 6.0) return p + (q - p) * 6.0 * t; | |
| 55 | ✗ | if (t < 1.0 / 2.0) return q; | |
| 56 | ✗ | if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6.0; | |
| 57 | ✗ | return p; | |
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | /** | ||
| 62 | * \brief Constructs a color from hue,saturation and lightness | ||
| 63 | * \param[in] h hue in [0.0,1.0] | ||
| 64 | * \param[in] s saturation in [0.0,1.0] | ||
| 65 | * \param[in] l lightness in [0.0,1.0] | ||
| 66 | */ | ||
| 67 | ✗ | Color hsl_to_rgb(double h, double s, double l) { | |
| 68 | double r,g,b; | ||
| 69 | ✗ | if (s == 0.0) { | |
| 70 | ✗ | return Color(l,l,l,1.0); | |
| 71 | } else { | ||
| 72 | ✗ | double q = l < 0.5 ? l * (1.0 + s) : l + s - l * s; | |
| 73 | ✗ | double p = 2.0 * l - q; | |
| 74 | ✗ | r = hue_to_rgb(p, q, h + 1.0 / 3.0); | |
| 75 | ✗ | g = hue_to_rgb(p, q, h); | |
| 76 | ✗ | b = hue_to_rgb(p, q, h - 1.0 / 3.0); | |
| 77 | } | ||
| 78 | ✗ | return Color(r,g,b,1.0); | |
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | namespace GEO { | ||
| 83 | |||
| 84 | ✗ | Color make_color_from_hsl(double h, double s, double l) { | |
| 85 | ✗ | return hsl_to_rgb(h,s,l); | |
| 86 | } | ||
| 87 | |||
| 88 | ✗ | Color make_random_color( | |
| 89 | double min_h, double max_h, | ||
| 90 | double min_s, double max_s, | ||
| 91 | double min_l, double max_l | ||
| 92 | ) { | ||
| 93 | ✗ | double h = Numeric::random_float64() * (max_h - min_h) + min_h; | |
| 94 | ✗ | double s = Numeric::random_float64() * (max_s - min_s) + min_s; | |
| 95 | ✗ | double l = Numeric::random_float64() * (max_l - min_l) + min_l; | |
| 96 | ✗ | return make_color_from_hsl(h,s,l); | |
| 97 | } | ||
| 98 | } | ||
| 99 |