aboutsummaryrefslogtreecommitdiff
#include "divergentcolorscale.h"

#include <cmath>

DivergentColorScale::DivergentColorScale(const Color &color1,
                                         const Color &colorMiddle,
                                         const Color &color2)
    : ColorScale{{color1, colorMiddle, color2}}
{
}

Color DivergentColorScale::color(float t) const
{
    if (t < m_min || t > m_max) {
        return Color();
    }

    // normalize t
    t /= std::max(fabs(m_max), fabs(m_min));

    if (t > 0.0f) {
        return ColorScale::lerp(m_colors[1], m_colors[2], t);
    }
    if (t < 0.0f) {
        return ColorScale::lerp(m_colors[1], m_colors[0], -t);
    }
    return m_colors[1];
}

DivergentColorScale DivergentColorScale::builtin(BuiltinDivergentColorScale scale)
{
    switch (scale) {
    case RedGrayBlue:
        return DivergentColorScale(Color(68,  68,  221),
                                   Color(189, 189, 189),
                                   Color(221,  68,  68));
        /*return ContinuousColorScale{
            Color(221,  68,  68),
            Color(220,  69,  68),
            Color(220,  70,  69),
            Color(220,  71,  70),
            Color(220,  73,  71),
            Color(220,  74,  72),
            Color(220,  75,  73),
            Color(220,  77,  74),
            Color(220,  78,  75),
            Color(220,  79,  76),
            Color(220,  80,  76),
            Color(220,  81,  77),
            Color(220,  83,  78),
            Color(220,  84,  79),
            Color(220,  85,  80),
            Color(220,  86,  81),
            Color(220,  87,  82),
            Color(220,  88,  83),
            Color(220,  89,  84),
            Color(220,  91,  85),
            Color(220,  92,  85),
            Color(220,  93,  86),
            Color(220,  94,  87),
            Color(220,  95,  88),
            Color(220,  96,  89),
            Color(220,  97,  90),
            Color(220,  98,  91),
            Color(219,  99,  92),
            Color(219, 100,  93),
            Color(219, 101,  94),
            Color(219, 102,  95),
            Color(219, 103,  95),
            Color(219, 104,  96),
            Color(219, 105,  97),
            Color(219, 106,  98),
            Color(219, 107, 99),
            Color(219, 108, 100),
            Color(218, 109, 101),
            Color(218, 110, 102),
            Color(218, 111, 103),
            Color(218, 112, 104),
            Color(218, 113, 105),
            Color(218, 114, 106),
            Color(218, 115, 107),
            Color(218, 116, 108),
            Color(217, 117, 108),
            Color(217, 118, 109),
            Color(217, 119, 110),
            Color(217, 120, 111),
            Color(217, 121, 112),
            Color(217, 122, 113),
            Color(217, 123, 114),
            Color(216, 124, 115),
            Color(216, 125, 116),
            Color(216, 126, 117),
            Color(216, 127, 118),
            Color(216, 127, 119),
            Color(215, 128, 120),
            Color(215, 129, 121),
            Color(215, 130, 122),
            Color(215, 131, 123),
            Color(215, 132, 124),
            Color(214, 133, 125),
            Color(214, 134, 126),
            Color(214, 135, 127),
            Color(214, 136, 127),
            Color(214, 137, 128),
            Color(213, 138, 129),
            Color(213, 138, 130),
            Color(213, 139, 131),
            Color(213, 140, 132),
            Color(212, 141, 133),
            Color(212, 142, 134),
            Color(212, 143, 135),
            Color(212, 144, 136),
            Color(211, 145, 137),
            Color(211, 146, 138),
            Color(211, 146, 139),
            Color(211, 147, 140),
            Color(210, 148, 141),
            Color(210, 149, 142),
            Color(210, 150, 143),
            Color(209, 151, 144),
            Color(209, 152, 145),
            Color(209, 153, 146),
            Color(208, 153, 147),
            Color(208, 154, 148),
            Color(208, 155, 149),
            Color(207, 156, 150),
            Color(207, 157, 151),
            Color(207, 158, 152),
            Color(206, 159, 153),
            Color(206, 160, 154),
            Color(206, 160, 155),
            Color(205, 161, 156),
            Color(205, 162, 157),
            Color(205, 163, 158),
            Color(204, 164, 159),
            Color(204, 165, 160),
            Color(203, 166, 161),
            Color(203, 166, 162),
            Color(203, 167, 163),
            Color(202, 168, 164),
            Color(202, 169, 165),
            Color(201, 170, 166),
            Color(201, 171, 167),
            Color(201, 171, 168),
            Color(200, 172, 169),
            Color(200, 173, 170),
            Color(199, 174, 171),
            Color(199, 175, 172),
            Color(198, 176, 173),
            Color(198, 177, 174),
            Color(197, 177, 175),
            Color(197, 178, 176),
            Color(196, 179, 177),
            Color(196, 180, 178),
            Color(195, 181, 179),
            Color(195, 182, 180),
            Color(194, 182, 181),
            Color(194, 183, 182),
            Color(193, 184, 183),
            Color(193, 185, 184),
            Color(192, 186, 185),
            Color(192, 187, 186),
            Color(191, 187, 187),
            Color(190, 188, 188),
            Color(190, 189, 189),
            Color(189, 189, 190),
            Color(189, 188, 190),
            Color(188, 187, 190),
            Color(187, 186, 191),
            Color(187, 185, 191),
            Color(186, 184, 191),
            Color(185, 183, 191),
            Color(185, 182, 192),
            Color(184, 181, 192),
            Color(184, 180, 192),
            Color(183, 179, 193),
            Color(182, 178, 193),
            Color(182, 177, 193),
            Color(181, 176, 194),
            Color(180, 175, 194),
            Color(180, 174, 194),
            Color(179, 173, 194),
            Color(178, 172, 195),
            Color(178, 171, 195),
            Color(177, 170, 195),
            Color(176, 169, 196),
            Color(176, 168, 196),
            Color(175, 167, 196),
            Color(174, 166, 196),
            Color(174, 165, 197),
            Color(173, 164, 197),
            Color(172, 163, 197),
            Color(172, 162, 197),
            Color(171, 161, 198),
            Color(170, 160, 198),
            Color(170, 159, 198),
            Color(169, 158, 199),
            Color(168, 157, 199),
            Color(167, 156, 199),
            Color(167, 155, 199),
            Color(166, 155, 200),
            Color(165, 154, 200),
            Color(165, 153, 200),
            Color(164, 152, 200),
            Color(163, 151, 201),
            Color(162, 150, 201),
            Color(162, 149, 201),
            Color(161, 148, 201),
            Color(160, 147, 202),
            Color(159, 146, 202),
            Color(159, 145, 202),
            Color(158, 144, 202),
            Color(157, 143, 203),
            Color(156, 142, 203),
            Color(155, 141, 203),
            Color(155, 140, 203),
            Color(154, 139, 204),
            Color(153, 138, 204),
            Color(152, 137, 204),
            Color(152, 136, 204),
            Color(151, 135, 205),
            Color(150, 134, 205),
            Color(149, 133, 205),
            Color(148, 132, 205),
            Color(147, 131, 206),
            Color(147, 130, 206),
            Color(146, 129, 206),
            Color(145, 128, 206),
            Color(144, 128, 207),
            Color(143, 127, 207),
            Color(142, 126, 207),
            Color(142, 125, 207),
            Color(141, 124, 208),
            Color(140, 123, 208),
            Color(139, 122, 208),
            Color(138, 121, 208),
            Color(137, 120, 209),
            Color(136, 119, 209),
            Color(135, 118, 209),
            Color(134, 117, 209),
            Color(133, 116, 209),
            Color(133, 115, 210),
            Color(132, 114, 210),
            Color(131, 113, 210),
            Color(130, 112, 210),
            Color(129, 111, 211),
            Color(128, 110, 211),
            Color(127, 109, 211),
            Color(126, 109, 211),
            Color(125, 108, 212),
            Color(124, 107, 212),
            Color(123, 106, 212),
            Color(122, 105, 212),
            Color(121, 104, 212),
            Color(120, 103, 213),
            Color(119, 102, 213),
            Color(118, 101, 213),
            Color(117, 100, 213),
            Color(115,  99, 213),
            Color(114,  98, 214),
            Color(113,  97, 214),
            Color(112,  96, 214),
            Color(111,  95, 214),
            Color(110,  94, 215),
            Color(109,  94, 215),
            Color(108,  93, 215),
            Color(106,  92, 215),
            Color(105,  91, 215),
            Color(104,  90, 216),
            Color(103,  89, 216),
            Color(101,  88, 216),
            Color(100,  87, 216),
            Color( 99,  86, 216),
            Color( 98,  85, 217),
            Color( 96,  84, 217),
            Color( 95,  83, 217),
            Color( 94,  82, 217),
            Color( 92,  81, 217),
            Color( 91,  80, 218),
            Color( 89,  80, 218),
            Color( 88,  79, 218),
            Color( 86,  78, 218),
            Color( 85,  77, 219),
            Color( 83,  76, 219),
            Color( 82,  75, 219),
            Color( 80,  74, 219),
            Color( 78,  73, 219),
            Color( 77,  72, 220),
            Color( 75,  71, 220),
            Color( 73,  70, 220),
            Color( 71,  69, 220),
            Color( 69,  68, 220),
            Color( 68,  68, 221)
        };*/
    default:
        return DivergentColorScale::builtin(DivergentColorScale::RedGrayBlue);
    }
}


DivergentColorScale *DivergentColorScale::builtin(BuiltinDivergentColorScale scale, void *)
{
    switch (scale) {
    case RedGrayBlue:
        return new DivergentColorScale(Color(68,  68,  221),
                                       Color(189, 189, 189),
                                       Color(221,  68,  68));
    default:
        return DivergentColorScale::builtin(DivergentColorScale::RedGrayBlue, nullptr);
    }
}