1 /* |
1 /* |
2 * LDForge: LDraw parts authoring CAD |
2 * LDForge: LDraw parts authoring CAD |
3 * Copyright (C) 2013 Santeri Piippo |
3 * Copyright (C) 2013 Santeri Piippo |
4 * |
4 * |
5 * This program is free software: you can redistribute it and/or modify |
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 |
6 * it under the terms of the GNU General Public License as published by |
7 * the Free Software Foundation, either version 3 of the License, or |
7 * the Free Software Foundation, either version 3 of the License, or |
8 * (at your option) any later version. |
8 * (at your option) any later version. |
9 * |
9 * |
10 * This program is distributed in the hope that it will be useful, |
10 * This program is distributed in the hope that it will be useful, |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 * GNU General Public License for more details. |
13 * GNU General Public License for more details. |
14 * |
14 * |
15 * You should have received a copy of the GNU General Public License |
15 * You should have received a copy of the GNU General Public License |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 */ |
17 */ |
18 |
18 |
19 #include "common.h" |
19 #include "common.h" |
40 col = new color; |
41 col = new color; |
41 col->hexcode = "#000000"; |
42 col->hexcode = "#000000"; |
42 col->edgeColor = col->faceColor = Qt::black; |
43 col->edgeColor = col->faceColor = Qt::black; |
43 g_LDColors[edgecolor] = col; |
44 g_LDColors[edgecolor] = col; |
44 |
45 |
45 parseLDConfig (); |
46 parseLDConfig(); |
46 } |
47 } |
47 |
48 |
48 // ============================================================================= |
49 // ============================================================================= |
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
50 // ============================================================================= |
51 // ============================================================================= |
51 color* getColor (short dColorNum) { |
52 color* getColor( short colnum ) |
|
53 { |
52 // Check bounds |
54 // Check bounds |
53 if (dColorNum < 0 || dColorNum >= MAX_COLORS) |
55 if( colnum < 0 || colnum >= MAX_COLORS ) |
54 return null; |
56 return null; |
55 |
57 |
56 return g_LDColors[dColorNum]; |
58 return g_LDColors[colnum]; |
57 } |
59 } |
58 |
60 |
59 // ============================================================================= |
61 // ============================================================================= |
60 uchar luma (QColor& col) { |
62 uchar luma( QColor& col ) |
61 return (0.2126f * col.red ()) + |
63 { |
62 (0.7152f * col.green ()) + |
64 return ( 0.2126f * col.red()) + |
63 (0.0722f * col.blue ()); |
65 ( 0.7152f * col.green()) + |
|
66 ( 0.0722f * col.blue() ); |
64 } |
67 } |
65 |
68 |
66 // ============================================================================= |
69 // ============================================================================= |
67 // Helper function for parseLDConfig |
70 // Helper function for parseLDConfig |
68 static bool parseLDConfigTag (StringParser& pars, char const* tag, str& val) { |
71 static bool parseLDConfigTag( StringParser& pars, char const* tag, str& val ) |
|
72 { |
69 short pos; |
73 short pos; |
70 |
74 |
71 // Try find the token and get its position |
75 // Try find the token and get its position |
72 if (!pars.findToken (pos, tag, 1)) |
76 if( !pars.findToken( pos, tag, 1 )) |
73 return false; |
77 return false; |
74 |
78 |
75 // Get the token after it and store it into val |
79 // Get the token after it and store it into val |
76 return pars.getToken (val, pos + 1); |
80 return pars.getToken( val, pos + 1 ); |
77 } |
81 } |
78 |
82 |
79 // ============================================================================= |
83 // ============================================================================= |
80 void parseLDConfig () { |
84 void parseLDConfig() |
81 File* f = openLDrawFile ("LDConfig.ldr", false); |
85 { |
|
86 File* f = openLDrawFile( "LDConfig.ldr", false ); |
82 |
87 |
83 if ( !f ) |
88 if( !f ) |
84 { |
89 { |
85 critical( fmt( QObject::tr( "Unable to open LDConfig.ldr for parsing! (%1)" ), strerror( errno ))); |
90 critical( fmt( QObject::tr( "Unable to open LDConfig.ldr for parsing! (%1)" ), strerror( errno )) ); |
86 delete f; |
91 delete f; |
87 return; |
92 return; |
88 } |
93 } |
89 |
94 |
90 // Read in the lines |
95 // Read in the lines |
91 for( str line : *f ) |
96 for( str line : *f ) |
92 { |
97 { |
93 if (line.length () == 0 || line[0] != '0') |
98 if( line.length() == 0 || line[0] != '0' ) |
94 continue; // empty or illogical |
99 continue; // empty or illogical |
95 |
100 |
96 line.remove ('\r'); |
101 line.remove( '\r' ); |
97 line.remove ('\n'); |
102 line.remove( '\n' ); |
98 |
103 |
99 // Parse the line |
104 // Parse the line |
100 StringParser pars (line, ' '); |
105 StringParser pars( line, ' ' ); |
101 |
106 |
102 short code = 0, alpha = 255; |
107 short code = 0, alpha = 255; |
103 str name, facename, edgename, valuestr; |
108 str name, facename, edgename, valuestr; |
104 |
109 |
105 // Check 0 !COLOUR, parse the name |
110 // Check 0 !COLOUR, parse the name |
106 if (!pars.tokenCompare (0, "0") || !pars.tokenCompare (1, "!COLOUR") || !pars.getToken (name, 2)) |
111 if( !pars.tokenCompare( 0, "0" ) || !pars.tokenCompare( 1, "!COLOUR" ) || !pars.getToken( name, 2 )) |
107 continue; |
112 continue; |
108 |
113 |
109 // Replace underscores in the name with spaces for readability |
114 // Replace underscores in the name with spaces for readability |
110 name.replace ("_", " "); |
115 name.replace( "_", " " ); |
111 |
116 |
112 // Get the CODE tag |
117 // Get the CODE tag |
113 if (!parseLDConfigTag (pars, "CODE", valuestr)) |
118 if( !parseLDConfigTag( pars, "CODE", valuestr )) |
114 continue; |
119 continue; |
115 |
120 |
116 if (!isNumber (valuestr)) |
121 if( !isNumber( valuestr )) |
117 continue; // not a number |
122 continue; // not a number |
118 |
123 |
119 // Ensure that the code is within [0 - 511] |
124 // Ensure that the code is within [0 - 511] |
120 bool ok; |
125 bool ok; |
121 code = valuestr.toShort (&ok); |
126 code = valuestr.toShort( &ok ); |
122 if (!ok || code < 0 || code >= 512) |
127 |
|
128 if( !ok || code < 0 || code >= 512 ) |
123 continue; |
129 continue; |
124 |
130 |
125 // VALUE and EDGE tags |
131 // VALUE and EDGE tags |
126 if (!parseLDConfigTag (pars, "VALUE", facename) || !parseLDConfigTag (pars, "EDGE", edgename)) |
132 if( !parseLDConfigTag( pars, "VALUE", facename ) || !parseLDConfigTag( pars, "EDGE", edgename )) |
127 continue; |
133 continue; |
128 |
134 |
129 // Ensure that our colors are correct |
135 // Ensure that our colors are correct |
130 QColor faceColor (facename), |
136 QColor faceColor( facename ), |
131 edgeColor (edgename); |
137 edgeColor( edgename ); |
132 |
138 |
133 if (!faceColor.isValid () || !edgeColor.isValid ()) |
139 if( !faceColor.isValid() || !edgeColor.isValid() ) |
134 continue; |
140 continue; |
135 |
141 |
136 // Parse alpha if given. |
142 // Parse alpha if given. |
137 if (parseLDConfigTag (pars, "ALPHA", valuestr)) |
143 if( parseLDConfigTag( pars, "ALPHA", valuestr )) |
138 alpha = clamp<short> (valuestr.toShort (), 0, 255); |
144 alpha = clamp<short> ( valuestr.toShort(), 0, 255 ); |
139 |
145 |
140 color* col = new color; |
146 color* col = new color; |
141 col->name = name; |
147 col->name = name; |
142 col->faceColor = faceColor; |
148 col->faceColor = faceColor; |
143 col->edgeColor = edgeColor; |
149 col->edgeColor = edgeColor; |
144 col->hexcode = facename; |
150 col->hexcode = facename; |
145 col->faceColor.setAlpha (alpha); |
151 col->faceColor.setAlpha( alpha ); |
146 col->index = code; |
152 col->index = code; |
147 g_LDColors[code] = col; |
153 g_LDColors[code] = col; |
148 } |
154 } |
149 |
155 |
150 delete f; |
156 delete f; |