PineForge unreleased
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
color.hpp
Go to the documentation of this file.
1#pragma once
2#include <cmath>
3#include <cstdint>
4
5namespace pineforge {
6
7// A color is 0xAARRGGBB: alpha is opacity, 0xFF opaque. The named constants
8// are TradingView's Pine v6 values; v5 spells three differently (color.red
9// #FF5252, color.teal #00897B, color.yellow #FFEB3B), which is the
10// transpiler's to lower (lab tv tapes w11-color-v6-eth15 / w11-color-v5-eth15,
11// tests/fixtures/color_tv).
12namespace pine_color {
13 constexpr int64_t red = 0xFFF23645;
14 constexpr int64_t green = 0xFF4CAF50;
15 constexpr int64_t blue = 0xFF2962FF;
16 constexpr int64_t white = 0xFFFFFFFF;
17 constexpr int64_t black = 0xFF363A45;
18 constexpr int64_t yellow = 0xFFFDD835;
19 constexpr int64_t orange = 0xFFFF9800;
20 constexpr int64_t purple = 0xFF9C27B0;
21 constexpr int64_t aqua = 0xFF00BCD4;
22 constexpr int64_t gray = 0xFF787B86;
23 constexpr int64_t lime = 0xFF00E676;
24 constexpr int64_t maroon = 0xFF880E4F;
25 constexpr int64_t navy = 0xFF311B92;
26 constexpr int64_t olive = 0xFF808000;
27 constexpr int64_t silver = 0xFFB2B5BE;
28 constexpr int64_t teal = 0xFF089981;
29 constexpr int64_t fuchsia = 0xFFE040FB;
30 // Transparency t (0 opaque .. 100 invisible) is stored as the alpha byte
31 // nearest 255 * (100 - t) / 100, and color.t reads a byte a back as
32 // 100 - a * 100 / 255 rounded to the nearest whole number, so a whole t
33 // reads back as itself (w11-color-v6-eth15's sweep; w11-color-alpha-eth15
34 // reads all 256 bytes). A t outside 0 .. 100 stores the nearer end, and
35 // a NaN t is 100, what the transpiler passes for an na one.
36 inline int64_t new_color(int64_t c, double transp) {
37 const double opacity = std::floor(255.0 * (100.0 - transp) / 100.0 + 0.5);
38 const int64_t alpha = !(opacity > 0.0) ? 0 : opacity >= 255.0 ? 255
39 : static_cast<int64_t>(opacity);
40 return (c & 0x00FFFFFF) | (alpha << 24);
41 }
42 inline int r(int64_t c) { return (int)((c >> 16) & 0xFF); }
43 inline int g(int64_t c) { return (int)((c >> 8) & 0xFF); }
44 inline int b(int64_t c) { return (int)(c & 0xFF); }
45 inline int t(int64_t c) { return 100 - (int)((((c >> 24) & 0xFF) * 200 + 255) / 510); }
46}
47
48} // namespace pineforge
int g(int64_t c)
Definition color.hpp:43
constexpr int64_t yellow
Definition color.hpp:18
constexpr int64_t green
Definition color.hpp:14
int t(int64_t c)
Definition color.hpp:45
constexpr int64_t orange
Definition color.hpp:19
constexpr int64_t teal
Definition color.hpp:28
int b(int64_t c)
Definition color.hpp:44
constexpr int64_t aqua
Definition color.hpp:21
int r(int64_t c)
Definition color.hpp:42
constexpr int64_t red
Definition color.hpp:13
constexpr int64_t lime
Definition color.hpp:23
constexpr int64_t gray
Definition color.hpp:22
constexpr int64_t black
Definition color.hpp:17
constexpr int64_t olive
Definition color.hpp:26
constexpr int64_t blue
Definition color.hpp:15
constexpr int64_t silver
Definition color.hpp:27
constexpr int64_t white
Definition color.hpp:16
constexpr int64_t navy
Definition color.hpp:25
constexpr int64_t purple
Definition color.hpp:20
constexpr int64_t fuchsia
Definition color.hpp:29
int64_t new_color(int64_t c, double transp)
Definition color.hpp:36
constexpr int64_t maroon
Definition color.hpp:24