Debian Codegraph @

src/gui/gradient.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "gui.h"
21 #include "imgui.h"
22 #include <fmt/printf.h>
23 #include <math.h>
24
25 ImU32 Gradient2D::get(float x, float y) {
26 int xi=round(x*width);
27 int yi=round(y*height);
28 if (xi<0) xi=0;
29 if (xi>=(int)width) xi=width-1;
30 if (yi<0) yi=0;
31 if (yi>=(int)height) yi=height-1;
32 return grad[yi*width+xi];
33 }
34
35 String Gradient2D::toString() {
36 String ret=fmt::sprintf("GRAD #%.2X%.2X%.2X%.2X",(unsigned char)(bgColor.x*255.0f),(unsigned char)(bgColor.y*255.0f),(unsigned char)(bgColor.z*255.0f),(unsigned char)(bgColor.w*255.0f));
37 for (Gradient2DPoint& i: points) {
38 ret+=fmt::sprintf(" %f,%f:%f,%f:#%.2X%.2X%.2X%.2X",i.x,i.y,i.distance,i.spread,(unsigned char)(i.color.x*255.0f),(unsigned char)(i.color.y*255.0f),(unsigned char)(i.color.z*255.0f),(unsigned char)(i.color.w*255.0f));
39 }
40 return ret;
41 }
42
43 bool Gradient2D::fromString(String val) {
44 std::vector<String> split;
45 String cur;
46 for (char i: val) {
47 if (i==' ') {
48 if (!cur.empty()) {
49 split.push_back(cur);
50 cur="";
51 }
52 } else {
53 cur+=i;
54 }
55 }
56 if (!cur.empty()) {
57 split.push_back(cur);
58 }
59
60 if (split.size()<2) return false;
61
62 if (split[0]!="GRAD") return false;
63
64 ImU32 bgColorH=0;
65 if (sscanf(split[1].c_str(),"#%X",&bgColorH)!=1) return false;
66 bgColorH=(bgColorH>>24)|((bgColorH>>8)&0xff00)|((bgColorH<<8)&0xff0000)|(bgColorH<<24);
67
68 bgColor=ImGui::ColorConvertU32ToFloat4(bgColorH);
69
70 for (size_t i=2; i<split.size(); i++) {
71 Gradient2DPoint point;
72 ImU32 colorH=0;
73 if (sscanf(split[i].c_str(),"%f,%f:%f,%f:#%X",&point.x,&point.y,&point.distance,&point.spread,&colorH)!=5) {
74 return false;
75 }
76 colorH=(colorH>>24)|((colorH>>8)&0xff00)|((colorH<<8)&0xff0000)|(colorH<<24);
77
78 point.color=ImGui::ColorConvertU32ToFloat4(colorH);
79 points.push_back(point);
80 }
81 return true;
82 }
83
84 void Gradient2D::render() {
85 ImU32* g=grad.get();
86 ImU32 bgColorU=ImGui::ColorConvertFloat4ToU32(bgColor);
87
88 // 1. fill with background color
89 for (size_t i=0; i<width*height; i++) {
90 g[i]=bgColorU;
91 }
92
93 // 2. insert points
94 for (Gradient2DPoint& i: points) {
95 float pDistSquared=i.distance*i.distance;
96 for (size_t j=0; j<height; j++) {
97 float jFloat=(float)j/(float)height;
98 float distY=jFloat-i.y;
99 for (size_t k=0; k<width; k++) {
100 float kFloat=(float)k/(float)width;
101 float distX=kFloat-i.x;
102 float distSquared=(distX*distX)+(distY*distY)-(i.spread*i.spread);
103 if (distSquared<0) distSquared=0;
104
105 if (distSquared>=pDistSquared) continue;
106
107 float dist=(1.0-(sqrt(distSquared)/i.distance));
108 if (dist<0) dist=0;
109 if (dist>1) dist=1;
110
111 ImU32 shadeColor=ImGui::ColorConvertFloat4ToU32(
112 ImVec4(
113 i.color.x*i.color.w*dist,
114 i.color.y*i.color.w*dist,
115 i.color.z*i.color.w*dist,
116 1.0f
117 )
118 );
119
120 ImU32 origColor=g[j*width+k];
121 g[j*width+k]=(
122 (MIN( 0xff, (origColor&0xff) + (shadeColor&0xff) )) | // R
123 (MIN( 0xff00, (origColor&0xff00) + (shadeColor&0xff00) )) | // G
124 (MIN(0xff0000,(origColor&0xff0000)+(shadeColor&0xff0000))) | // B
125 (origColor&0xff000000) // A
126 );
127 }
128 }
129 }
130 }