src/colors.cpp

changeset 406
5371baa17346
parent 364
7c3af0a6f8ab
child 421
7d26db0be944
equal deleted inserted replaced
405:b8c813906ac0 406:5371baa17346
19 #include "common.h" 19 #include "common.h"
20 #include "colors.h" 20 #include "colors.h"
21 #include "file.h" 21 #include "file.h"
22 #include "misc.h" 22 #include "misc.h"
23 #include "gui.h" 23 #include "gui.h"
24 #include <qcolor.h> 24 #include "ldconfig.h"
25 #include <QColor>
25 26
26 static LDColor* g_LDColors[MAX_COLORS]; 27 static LDColor* g_LDColors[MAX_COLORS];
27 28
28 void initColors() 29 void initColors() {
29 {
30 print( "%1: initializing color information.\n", __func__ ); 30 print( "%1: initializing color information.\n", __func__ );
31 31
32 LDColor* col; 32 LDColor* col;
33 33
34 // Always make sure there's 16 and 24 available. They're special like that. 34 // Always make sure there's 16 and 24 available. They're special like that.
45 45
46 parseLDConfig(); 46 parseLDConfig();
47 } 47 }
48 48
49 // ============================================================================= 49 // =============================================================================
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 50 // -----------------------------------------------------------------------------
51 // ============================================================================= 51 LDColor* getColor( short colnum ) {
52 LDColor* getColor( short colnum )
53 {
54 // Check bounds 52 // Check bounds
55 if( colnum < 0 || colnum >= MAX_COLORS ) 53 if( colnum < 0 || colnum >= MAX_COLORS )
56 return null; 54 return null;
57 55
58 return g_LDColors[colnum]; 56 return g_LDColors[colnum];
57 }
58
59 // =============================================================================
60 // -----------------------------------------------------------------------------
61 void setColor( short colnum, LDColor* col ) {
62 if( colnum < 0 || colnum >= MAX_COLORS )
63 return;
64
65 g_LDColors[colnum] = col;
59 } 66 }
60 67
61 // ============================================================================= 68 // =============================================================================
62 uchar luma( QColor& col ) 69 uchar luma( QColor& col )
63 { 70 {
64 return ( 0.2126f * col.red()) + 71 return ( 0.2126f * col.red()) +
65 ( 0.7152f * col.green()) + 72 ( 0.7152f * col.green()) +
66 ( 0.0722f * col.blue() ); 73 ( 0.0722f * col.blue() );
67 } 74 }
68
69 // =============================================================================
70 // Helper function for parseLDConfig
71 static bool parseLDConfigTag( StringParser& pars, char const* tag, str& val )
72 {
73 short pos;
74
75 // Try find the token and get its position
76 if( !pars.findToken( pos, tag, 1 ))
77 return false;
78
79 // Get the token after it and store it into val
80 return pars.getToken( val, pos + 1 );
81 }
82
83 // =============================================================================
84 void parseLDConfig()
85 {
86 File* f = openLDrawFile( "LDConfig.ldr", false );
87
88 if( !f )
89 {
90 critical( fmt( QObject::tr( "Unable to open LDConfig.ldr for parsing! (%1)" ), strerror( errno )) );
91 delete f;
92 return;
93 }
94
95 // Read in the lines
96 for( str line : *f )
97 {
98 if( line.length() == 0 || line[0] != '0' )
99 continue; // empty or illogical
100
101 line.remove( '\r' );
102 line.remove( '\n' );
103
104 // Parse the line
105 StringParser pars( line, ' ' );
106
107 short code = 0, alpha = 255;
108 str name, facename, edgename, valuestr;
109
110 // Check 0 !COLOUR, parse the name
111 if( !pars.tokenCompare( 0, "0" ) || !pars.tokenCompare( 1, "!COLOUR" ) || !pars.getToken( name, 2 ))
112 continue;
113
114 // Replace underscores in the name with spaces for readability
115 name.replace( "_", " " );
116
117 // Get the CODE tag
118 if( !parseLDConfigTag( pars, "CODE", valuestr ))
119 continue;
120
121 if( !isNumber( valuestr ))
122 continue; // not a number
123
124 // Ensure that the code is within [0 - 511]
125 bool ok;
126 code = valuestr.toShort( &ok );
127
128 if( !ok || code < 0 || code >= 512 )
129 continue;
130
131 // VALUE and EDGE tags
132 if( !parseLDConfigTag( pars, "VALUE", facename ) || !parseLDConfigTag( pars, "EDGE", edgename ))
133 continue;
134
135 // Ensure that our colors are correct
136 QColor faceColor( facename ),
137 edgeColor( edgename );
138
139 if( !faceColor.isValid() || !edgeColor.isValid() )
140 continue;
141
142 // Parse alpha if given.
143 if( parseLDConfigTag( pars, "ALPHA", valuestr ))
144 alpha = clamp<short> ( valuestr.toShort(), 0, 255 );
145
146 LDColor* col = new LDColor;
147 col->name = name;
148 col->faceColor = faceColor;
149 col->edgeColor = edgeColor;
150 col->hexcode = facename;
151 col->faceColor.setAlpha( alpha );
152 col->index = code;
153 g_LDColors[code] = col;
154 }
155
156 delete f;
157 }

mercurial