File: test-gradient.cc

package info (click to toggle)
conky 1.22.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,376 kB
  • sloc: cpp: 64,271; ansic: 18,382; python: 813; xml: 324; sh: 248; makefile: 143; javascript: 139
file content (139 lines) | stat: -rw-r--r-- 4,148 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 *
 * Conky, a system monitor, based on torsmo
 *
 * Any original torsmo code is licensed under the BSD license
 *
 * All code written since the fork of torsmo is licensed under the GPL
 *
 * Please see COPYING for details
 *
 * Copyright (c) 2005-2024 Brenden Matthews, Philip Kovacs, et. al.
 *	(see AUTHORS)
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "catch2/catch.hpp"

#include <conky.h>
#include <content/colours.hh>
#include <content/gradient.hh>

#include <iomanip>
#include <iostream>

const int width = 4;
const Colour colour = Colour::from_argb32(0xff996633);  // brown
const long expected_hue = 256;
const long expected_value = 0x99;   // max(0x99, 0x66, 0x33)
const long expected_chroma = 0x66;  // (0x99 - 0x33)
const long expected_luma = 20712665L;
const long expected_saturation = 122880L;
const long expected_red = 0x99;
const long expected_green = 0x66;
const long expected_blue = 0x33;

const long full_scale = conky::gradient_factory::SCALE360;

std::ostream& operator<<(std::ostream& s, const Colour& c) {
  s << '#';
  s << std::setfill('0');
  s << std::setw(2);
  s << std::setbase(16);
  s << (int)c.alpha << (int)c.red << (int)c.green << (int)c.blue;
  return s;
}

TEST_CASE("gradient_factory::convert_from_rgb returns correct value") {
#ifdef BUILD_X11
  state = nullptr;
#endif
  SECTION("rgb_gradient_factory") {
    auto factory = new conky::rgb_gradient_factory(width, colour, colour);
    long result[3];

    factory->convert_from_rgb(colour, result);

    SECTION("red") { REQUIRE(result[0] == expected_red * full_scale); }
    SECTION("green") { REQUIRE(result[1] == expected_green * full_scale); }
    SECTION("blue") { REQUIRE(result[2] == expected_blue * full_scale); }

    delete factory;
  }

  SECTION("hsv_gradient_factory") {
    auto factory = new conky::hsv_gradient_factory(width, colour, colour);
    long result[3];

    factory->convert_from_rgb(colour, result);

    SECTION("hue") { REQUIRE(result[0] == expected_hue * 60); }
    SECTION("saturation") { REQUIRE(result[1] == expected_saturation); }
    SECTION("value") { REQUIRE(result[2] == expected_value * full_scale); }

    delete factory;
  }

  SECTION("hcl_gradient_factory") {
    auto factory = new conky::hcl_gradient_factory(width, colour, colour);
    long result[3];

    factory->convert_from_rgb(colour, result);

    SECTION("hue") { REQUIRE(result[0] == expected_hue * 60); }
    SECTION("chroma") { REQUIRE(result[1] == expected_chroma * full_scale); }
    SECTION("luma") { REQUIRE(result[2] == expected_luma); }

    delete factory;
  }
}

TEST_CASE(
    "gradient_factory should convert to and from rgb "
    "and get the initial value") {
  SECTION("rgb_gradient_factory") {
    long tmp[3];
    auto factory = new conky::rgb_gradient_factory(width, colour, colour);
    factory->convert_from_rgb(colour, tmp);
    auto result = factory->convert_to_rgb(tmp);

    REQUIRE(result == colour);

    delete factory;
  }

  SECTION("hsv_gradient_factory") {
    long tmp[3];
    auto factory = new conky::hsv_gradient_factory(width, colour, colour);
    factory->convert_from_rgb(colour, tmp);
    auto result = factory->convert_to_rgb(tmp);

    REQUIRE(result == colour);

    delete factory;
  }

  SECTION("hcl_gradient_factory") {
    long tmp[3];
    auto factory = new conky::hcl_gradient_factory(width, colour, colour);
    factory->convert_from_rgb(colour, tmp);
    auto result = factory->convert_to_rgb(tmp);

    REQUIRE(result == colour);

    delete factory;
  }
}