aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dxf/imp_dxf_color.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dxf/imp_dxf_color.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dxf/imp_dxf_color.c130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dxf/imp_dxf_color.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dxf/imp_dxf_color.c
new file mode 100644
index 0000000..6ffdf87
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dxf/imp_dxf_color.c
@@ -0,0 +1,130 @@
1/* $Id:$ */
2
3/*
4 libg3d - 3D object loading library
5
6 Copyright (C) 2005-2009 Markus Dahms <mad@automagically.de>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21*/
22#include <string.h>
23
24#include <g3d/types.h>
25#include <g3d/material.h>
26
27static G3DMaterial *get_material(G3DModel *model, const gchar *cname)
28{
29 G3DMaterial *material;
30 GSList *item;
31
32 for(item = model->materials; item != NULL; item = item->next) {
33 material = item->data;
34 if(strcmp(material->name, cname) == 0)
35 return material;
36 }
37 return NULL;
38}
39
40#define DXF_COL_SET(rx, gx, bx) \
41 material->r = ((G3DFloat)(rx) / 255.0); \
42 material->g = ((G3DFloat)(gx) / 255.0); \
43 material->b = ((G3DFloat)(bx) / 255.0);
44
45/*
46 * based on GPL code from
47 * http://wiki.inkscape.org/wiki/index.php/SOC_Accepted_Proposals
48 */
49static gboolean set_aci(G3DMaterial *material, gint32 aci)
50{
51 G3DFloat r, g, b, h, s, l, m;
52 gint32 mod10;
53
54 if((aci < 10) || (aci > 249)) {
55 switch(aci) {
56 case 0: DXF_COL_SET(0x00, 0x00, 0x00); break;
57 case 1: DXF_COL_SET(0xFF, 0x00, 0x00); break;
58 case 2: DXF_COL_SET(0xFF, 0xFF, 0x00); break;
59 case 3: DXF_COL_SET(0x00, 0xFF, 0x00); break;
60 case 4: DXF_COL_SET(0x00, 0xFF, 0xFF); break;
61 case 5: DXF_COL_SET(0x00, 0x00, 0xFF); break;
62 case 6: DXF_COL_SET(0xFF, 0x00, 0xFF); break;
63 case 7: DXF_COL_SET(0xFF, 0xFF, 0xFF); break;
64 case 8: DXF_COL_SET(0x80, 0x80, 0x80); break;
65 case 9: DXF_COL_SET(0xC0, 0xC0, 0xC0); break;
66
67 case 250: DXF_COL_SET(0x33, 0x33, 0x33); break;
68 case 251: DXF_COL_SET(0x5B, 0x5B, 0x5B); break;
69 case 252: DXF_COL_SET(0x84, 0x84, 0x84); break;
70 case 253: DXF_COL_SET(0xAD, 0xAD, 0xAD); break;
71 case 254: DXF_COL_SET(0xD6, 0xD6, 0xD6); break;
72 case 255: DXF_COL_SET(0xFF, 0xFF, 0xFF); break;
73 default: break;
74 }
75 } else {
76 mod10 = aci % 10;
77 h = 1.5 * (aci - mod10 - 10);
78 s = ((aci % 2) ? 0.5 : 1.0);
79 if(mod10 < 2) l = 1.0;
80 else if(mod10 < 4) l = 0.8;
81 else if(mod10 < 6) l = 0.6;
82 else if(mod10 < 8) l = 0.5;
83 else l = 0.3;
84
85 if(h <= 120) {
86 r = (120 - h) / 60;
87 g = h / 60;
88 b = 0;
89 } else if(h <= 240) {
90 r = 0;
91 g = (240 - h) / 60;
92 b = (h - 120) / 60;
93 } else if(h <= 360) {
94 r = (h - 240) / 60;
95 g = 0;
96 b = (360 - h) / 60;
97 }
98 r = MIN(r, 1.0);
99 g = MIN(g, 1.0);
100 b = MIN(b, 1.0);
101 m = MAX(r, MAX(g, b));
102 material->r = (m - s * (m - r)) * l;
103 material->g = (m - s * (m - g)) * l;
104 material->b = (m - s * (m - b)) * l;
105 }
106 return TRUE;
107}
108
109G3DMaterial *dxf_color_get_material(G3DModel *model, gint32 aci)
110{
111 gchar *cname;
112 G3DMaterial *material;
113
114 cname = g_strdup_printf("color #%d", ABS(aci));
115 material = get_material(model, cname);
116 if(material != NULL) {
117 g_free(cname);
118 return material;
119 }
120 material = g3d_material_new();
121 material->name = cname;
122 model->materials = g_slist_append(model->materials, material);
123 set_aci(material, ABS(aci));
124#if DEBUG > 0
125 g_debug("| color: %.2f %.2f %.2f", material->r, material->g, material->b);
126#endif
127
128 return material;
129}
130