1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | #include <vrutil/vrColorBrewerFactory.h> |
---|
3 | #include <vrutil/vrColorBrewer.h> |
---|
4 | #include <string.h> |
---|
5 | #include <stdio.h> |
---|
6 | #include <stdlib.h> |
---|
7 | |
---|
8 | static const int COLOR_SCHEME_ORDER_SEQ[][9] = { {2, 5, 8, -1, -1, -1, -1, -1, -1}, |
---|
9 | {1, 4, 6, 9, -1, -1, -1, -1, -1}, |
---|
10 | {1, 4, 6, 8, 10, -1, -1, -1, -1}, |
---|
11 | {1, 3, 5, 6, 8, 10, -1, -1, -1}, |
---|
12 | {1, 3, 5, 6, 7, 9, 11, -1, -1}, |
---|
13 | {0, 2, 3, 5, 6, 7, 9, 11, -1}, |
---|
14 | {0, 2, 3, 5, 6, 7, 9, 10, 12} }; |
---|
15 | |
---|
16 | static const int COLOR_SCHEME_ORDER_DIV[][11] = { {10, 7, 4, -1, -1, -1, -1, -1, -1, -1, -1}, |
---|
17 | {12, 9, 5, 2, -1, -1, -1, -1, -1, -1, -1}, |
---|
18 | {12, 9, 7, 5, 2, -1, -1, -1, -1, -1, -1}, |
---|
19 | {13, 10, 8, 6, 4, 1, -1, -1, -1, -1, -1}, |
---|
20 | {13, 10, 8, 7, 6, 4, 1, -1, -1, -1}, |
---|
21 | {13, 11, 9, 8, 6, 5, 3, 1, -1, -1, -1}, |
---|
22 | {13, 11, 9, 8, 7, 6, 5, 3, 1, -1, -1}, |
---|
23 | {14, 13, 11, 9, 8, 6, 5, 3, 1, 0, -1}, |
---|
24 | {14, 13, 11, 9, 8, 7, 6, 5, 3, 1, 0} }; |
---|
25 | |
---|
26 | vrColorBrewerFactory* vrColorBrewerFactory::_instance = 0; |
---|
27 | |
---|
28 | |
---|
29 | vrColorBrewerFactory* vrColorBrewerFactory::getInstance() |
---|
30 | { |
---|
31 | if (_instance == 0) { |
---|
32 | _instance = new vrColorBrewerFactory(); |
---|
33 | _instance->loadColorBrewerList(); |
---|
34 | } |
---|
35 | return _instance; |
---|
36 | } |
---|
37 | |
---|
38 | vrColorBrewerFactory::vrColorBrewerFactory() |
---|
39 | : _currentColorBrewer(0) |
---|
40 | { |
---|
41 | } |
---|
42 | |
---|
43 | vrColorBrewer* vrColorBrewerFactory::chooseColorScheme(const std::string& scheme, int size) |
---|
44 | { |
---|
45 | std::vector<vrColorBrewer*>::iterator cbi; |
---|
46 | |
---|
47 | int newSize = size; |
---|
48 | for(cbi = colorList.begin(); (*cbi)->label != scheme && cbi != colorList.end(); cbi++) |
---|
49 | { |
---|
50 | printf("[%s]\n", (*cbi)->label.c_str()); |
---|
51 | } |
---|
52 | |
---|
53 | if (cbi == colorList.end()) |
---|
54 | { |
---|
55 | printf("color set not found\n"); |
---|
56 | return 0; |
---|
57 | } |
---|
58 | |
---|
59 | // INSOO |
---|
60 | //if (_currentColorBrewer) delete _currentColorBrewer; |
---|
61 | |
---|
62 | char label[100]; |
---|
63 | strcpy(label, scheme.c_str()); |
---|
64 | |
---|
65 | if((*cbi)->size == 13) |
---|
66 | { |
---|
67 | if(size > COLOR_SCHEME_SEQ_END) |
---|
68 | newSize = COLOR_SCHEME_SEQ_END; |
---|
69 | else if(size < COLOR_SCHEME_START) |
---|
70 | newSize = COLOR_SCHEME_START; |
---|
71 | |
---|
72 | _currentColorBrewer = new vrColorBrewer(newSize, label); |
---|
73 | for(int i = 0; i < newSize; i++) |
---|
74 | { |
---|
75 | for(int j = 0; j < 3; j++) |
---|
76 | _currentColorBrewer->colorScheme[i] = (*cbi)->colorScheme[COLOR_SCHEME_ORDER_SEQ[newSize - COLOR_SCHEME_START][i]]; |
---|
77 | } |
---|
78 | } |
---|
79 | else if((*cbi)->size == 15) |
---|
80 | { |
---|
81 | if(size > COLOR_SCHEME_DIV_END) |
---|
82 | newSize = COLOR_SCHEME_DIV_END; |
---|
83 | else if(size < COLOR_SCHEME_START) |
---|
84 | newSize = COLOR_SCHEME_START; |
---|
85 | |
---|
86 | _currentColorBrewer = new vrColorBrewer(newSize, label); |
---|
87 | |
---|
88 | for(int i = 0; i < newSize; i++) |
---|
89 | { |
---|
90 | _currentColorBrewer->colorScheme[i] = (*cbi)->colorScheme[COLOR_SCHEME_ORDER_DIV[newSize - COLOR_SCHEME_START][i]]; |
---|
91 | } |
---|
92 | } |
---|
93 | else |
---|
94 | { |
---|
95 | if(size > (*cbi)->size) |
---|
96 | newSize = (*cbi)->size; |
---|
97 | else if(size < 3) |
---|
98 | newSize = 3; |
---|
99 | |
---|
100 | _currentColorBrewer = new vrColorBrewer(newSize, label); |
---|
101 | |
---|
102 | for(int i = 0; i < newSize; i++) |
---|
103 | { |
---|
104 | _currentColorBrewer->colorScheme[i] = (*cbi)->colorScheme[i]; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | //this->initializeDivColorMap(); |
---|
109 | |
---|
110 | return _currentColorBrewer; |
---|
111 | } |
---|
112 | |
---|
113 | void vrColorBrewerFactory::loadColorBrewerList() |
---|
114 | { |
---|
115 | char label[20]; |
---|
116 | int size; |
---|
117 | double a, b, c; |
---|
118 | |
---|
119 | FILE* file = fopen("colorbrewer.txt", "rb"); |
---|
120 | char line[256]; |
---|
121 | |
---|
122 | if (file != 0) |
---|
123 | { |
---|
124 | |
---|
125 | int len; |
---|
126 | while(!feof(file)) |
---|
127 | { |
---|
128 | if (fgets(line, 256, file) == 0) break; |
---|
129 | |
---|
130 | len = strlen(line); |
---|
131 | if ((line[len - 1] == '\r') || (line[len - 1] == '\n')) |
---|
132 | { |
---|
133 | line[len - 1] = '\0'; |
---|
134 | } |
---|
135 | |
---|
136 | vrColorBrewer *cbo; |
---|
137 | |
---|
138 | strcpy(label, line); |
---|
139 | if (fgets(line, 256, file) == 0) break; |
---|
140 | |
---|
141 | size = atoi(line); |
---|
142 | cbo = new vrColorBrewer(size, label); |
---|
143 | |
---|
144 | for(int i = 0; i < size; i++) |
---|
145 | { |
---|
146 | if (fgets(line, 256, file) == 0) break; |
---|
147 | sscanf(line, "%lf,%lf,%lf", &a, &b, &c); |
---|
148 | |
---|
149 | cbo->colorScheme[i].r = (float) a; |
---|
150 | cbo->colorScheme[i].g = (float) b; |
---|
151 | cbo->colorScheme[i].b = (float) c; |
---|
152 | } |
---|
153 | |
---|
154 | colorList.push_back(cbo); |
---|
155 | } |
---|
156 | |
---|
157 | fclose(file); |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | printf("File Not Found - ColorBrewer"); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|