src/colors.cpp

changeset 211
8d35e631bef3
parent 189
ac2d3e8dd110
child 248
4431371f3ffe
equal deleted inserted replaced
210:58a3cbb49374 211:8d35e631bef3
54 54
55 return g_LDColors[dColorNum]; 55 return g_LDColors[dColorNum];
56 } 56 }
57 57
58 // ============================================================================= 58 // =============================================================================
59 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
60 // =============================================================================
61 static bool parseLDConfigTag (StringParser& pars, char const* tag, str& val) {
62 short pos;
63 if (!pars.findToken (pos, tag, 1))
64 return false;
65
66 return pars.getToken (val, pos + 1);
67 }
68
69 // =============================================================================
70 uchar luma (QColor& col) { 59 uchar luma (QColor& col) {
71 return (0.2126f * col.red ()) + 60 return (0.2126f * col.red ()) +
72 (0.7152f * col.green ()) + 61 (0.7152f * col.green ()) +
73 (0.0722f * col.blue ()); 62 (0.0722f * col.blue ());
74 } 63 }
75 64
76 // ============================================================================= 65 // =============================================================================
77 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 66 // Helper function for parseLDConfig
67 static bool parseLDConfigTag (StringParser& pars, char const* tag, str& val) {
68 short pos;
69
70 // Try find the token and get its position
71 if (!pars.findToken (pos, tag, 1))
72 return false;
73
74 // Get the token after it and store it into val
75 return pars.getToken (val, pos + 1);
76 }
77
78 // ============================================================================= 78 // =============================================================================
79 void parseLDConfig () { 79 void parseLDConfig () {
80 FILE* fp = openLDrawFile ("LDConfig.ldr", false); 80 FILE* fp = openLDrawFile ("LDConfig.ldr", false);
81 81
82 if (!fp) 82 if (!fp)
83 return; 83 return;
84 84
85 // Even though LDConfig.ldr is technically an LDraw file, parsing it as one 85 // Read in the lines
86 // would be overkill by any standard.
87 char buf[1024]; 86 char buf[1024];
88 while (fgets (buf, sizeof buf, fp)) { 87 while (fgets (buf, sizeof buf, fp)) {
89 if (strlen (buf) == 0 || buf[0] != '0') 88 if (strlen (buf) == 0 || buf[0] != '0')
90 continue; // empty or illogical 89 continue; // empty or illogical
91 90
91 // Use StringParser to parse the LDConfig.ldr file.
92 str line = str (buf).strip ({'\n', '\r'}); 92 str line = str (buf).strip ({'\n', '\r'});
93 StringParser pars (line, ' '); 93 StringParser pars (line, ' ');
94 94
95 short code = 0, alpha = 255; 95 short code = 0, alpha = 255;
96 str name, facename, edgename, valuestr; 96 str name, facename, edgename, valuestr;
100 continue; 100 continue;
101 101
102 // Replace underscores in the name with spaces for readability 102 // Replace underscores in the name with spaces for readability
103 name.replace ("_", " "); 103 name.replace ("_", " ");
104 104
105 // get the CODE tag 105 // Get the CODE tag
106 if (!parseLDConfigTag (pars, "CODE", valuestr)) 106 if (!parseLDConfigTag (pars, "CODE", valuestr))
107 continue; 107 continue;
108 108
109 // Ensure that the code is within range. must be within 0 - 512 109 if (!isNumber (valuestr))
110 continue; // not a number
111
112 // Ensure that the code is within [0 - 511]
110 code = atoi (valuestr); 113 code = atoi (valuestr);
111 if (code < 0 || code >= 512) 114 if (code < 0 || code >= 512)
112 continue; 115 continue;
113 116
114 // VALUE tag 117 // VALUE and EDGE tags
115 if (!parseLDConfigTag (pars, "VALUE", facename)) 118 if (!parseLDConfigTag (pars, "VALUE", facename) || !parseLDConfigTag (pars, "EDGE", edgename))
116 continue;
117
118 // EDGE tag
119 if (!parseLDConfigTag (pars, "EDGE", edgename))
120 continue; 119 continue;
121 120
122 // Ensure that our colors are correct 121 // Ensure that our colors are correct
123 QColor faceColor (facename.chars()), 122 QColor faceColor (facename.chars()),
124 edgeColor (edgename.chars()); 123 edgeColor (edgename.chars());

mercurial