source: trunk/packages/vizservers/nanovis/vrutil/vrColorBrewerFactory.cpp @ 2096

Last change on this file since 2096 was 2096, checked in by ldelgass, 13 years ago

Normalize line endings, set eol-style to native on *.cpp, *.h files

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