FTXUI 7.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
color_fuzzer.cpp
Go to the documentation of this file.
1// Copyright 2026 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <cstddef>
5#include <cstdint>
6#include <string>
9
10using namespace ftxui;
11
12namespace {
13
14uint8_t GeneratorByte(const char*& data, size_t& size) {
15 if (size == 0) {
16 return 0;
17 }
18 uint8_t out = static_cast<uint8_t>(data[0]);
19 data++;
20 size--;
21 return out;
22}
23
24float GeneratorFloat(const char*& data, size_t& size) {
25 if (size == 0) {
26 return 0.0f;
27 }
28 float out = float(static_cast<uint8_t>(data[0])) / 255.0f;
29 data++;
30 size--;
31 return out;
32}
33
34Color GeneratorColor(const char*& data, size_t& size) {
35 if (size < 4) {
36 return Color();
37 }
38 int type = GeneratorByte(data, size) % 5;
39 switch (type) {
40 case 0:
41 return Color(
42 static_cast<Color::Palette16>(GeneratorByte(data, size) % 16));
43 case 1:
44 return Color(static_cast<Color::Palette256>(GeneratorByte(data, size)));
45 case 2: {
46 uint8_t r = GeneratorByte(data, size);
47 uint8_t g = GeneratorByte(data, size);
48 uint8_t b = GeneratorByte(data, size);
49 return Color::RGB(r, g, b);
50 }
51 case 3: {
52 uint8_t h = GeneratorByte(data, size);
53 uint8_t s = GeneratorByte(data, size);
54 uint8_t v = GeneratorByte(data, size);
55 return Color::HSV(h, s, v);
56 }
57 case 4: {
58 uint8_t r = GeneratorByte(data, size);
59 uint8_t g = GeneratorByte(data, size);
60 uint8_t b = GeneratorByte(data, size);
61 uint8_t a = GeneratorByte(data, size);
62 return Color::RGBA(r, g, b, a);
63 }
64 default:
65 return Color();
66 }
67}
68
69} // namespace
70
71extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
72 if (size < 5) {
73 return 0;
74 }
75
76 // Read terminal color support
77 int support = GeneratorByte(data, size) % 4;
78 switch (support) {
79 case 0:
80 Terminal::SetColorSupport(Terminal::Color::Palette1);
81 break;
82 case 1:
83 Terminal::SetColorSupport(Terminal::Color::Palette16);
84 break;
85 case 2:
86 Terminal::SetColorSupport(Terminal::Color::Palette256);
87 break;
88 case 3:
89 Terminal::SetColorSupport(Terminal::Color::TrueColor);
90 break;
91 }
92
93 while (size > 1) {
94 int op = GeneratorByte(data, size) % 4;
95 switch (op) {
96 case 0: {
97 Color c = GeneratorColor(data, size);
98 bool is_background = GeneratorByte(data, size) % 2;
99 c.Print(is_background);
100 break;
101 }
102 case 1: {
103 Color c1 = GeneratorColor(data, size);
104 Color c2 = GeneratorColor(data, size);
105 float t = GeneratorFloat(data, size);
106 Color result = Color::Interpolate(t, c1, c2);
107 result.Print(true);
108 result.Print(false);
109 break;
110 }
111 case 2: {
112 Color c1 = GeneratorColor(data, size);
113 Color c2 = GeneratorColor(data, size);
114 Color result = Color::Blend(c1, c2);
115 result.Print(true);
116 result.Print(false);
117 break;
118 }
119 case 3: {
120 uint8_t h = GeneratorByte(data, size);
121 uint8_t s = GeneratorByte(data, size);
122 uint8_t v = GeneratorByte(data, size);
123 uint8_t a = GeneratorByte(data, size);
124 Color result = Color::HSVA(h, s, v, a);
125 result.Print(true);
126 break;
127 }
128 }
129 }
130
131 return 0;
132}
int LLVMFuzzerTestOneInput(const char *data, size_t size)
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
Color
Build a transparent color.
Definition elements.hpp:92