File: color.cpp

package info (click to toggle)
bowtie 1.2.3+dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 16,888 kB
  • sloc: cpp: 35,784; perl: 5,903; ansic: 1,247; sh: 1,180; python: 487; makefile: 426
file content (114 lines) | stat: -rw-r--r-- 2,031 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * color.cpp
 *
 *  Created on: Oct 18, 2009
 *      Author: Ben Langmead
 */

#include <iostream>
#include <sstream>
#include <string>
#include "color.h"

using namespace std;

/**
 * Set the console color.
 */
void setConsoleColor(int color) {
	cout << (char)0x1B << "[" << 0 << ";" << color + 30 << ";" << 0 + 40 << "m";
}

/**
 * Set the console color.
 */
void appendConsoleColor(string& s, int color) {
	s.push_back((char)0x1B);
	s.append("[0;");
	ostringstream ss;
	ss << (color + 30);
	s.append(ss.str());
	s.append(";40m");
}

/**
 * Print color character in the appropriate color to the console.
 * Console must support color.
 */
void printColor(char color) {
	char ch = ' ';
	switch(color) {
		case 'A':
		case '0':
		case 0:
			setConsoleColor(COLOR_BLUE);
			ch = '0'; break;
		case 'C':
		case '1':
		case 1:
			setConsoleColor(COLOR_GREEN);
			ch = '1'; break;
		case 'G':
		case '2':
		case 2:
			setConsoleColor(COLOR_YELLOW);
			ch = '2'; break;
		case 'T':
		case '3':
		case 3:
			setConsoleColor(COLOR_RED);
			ch = '3'; break;
		case 'N':
		case '4':
		case '.':
		case 4:
			setConsoleColor(COLOR_WHITE);
			ch = '.'; break;
		default:
			setConsoleColor(COLOR_WHITE);
			break;
	}
	cout << ch;
	setConsoleColor(COLOR_WHITE);
}

/**
 * Print color character in the appropriate color to the console.
 * Console must support color.
 */
void appendColor(string& s, char color) {
	char ch = ' ';
	switch(color) {
		case 'A':
		case '0':
		case 0:
			appendConsoleColor(s, COLOR_BLUE);
			ch = '0'; break;
		case 'C':
		case '1':
		case 1:
			appendConsoleColor(s, COLOR_GREEN);
			ch = '1'; break;
		case 'G':
		case '2':
		case 2:
			appendConsoleColor(s, COLOR_YELLOW);
			ch = '2'; break;
		case 'T':
		case '3':
		case 3:
			appendConsoleColor(s, COLOR_RED);
			ch = '3'; break;
		case 'N':
		case '4':
		case '.':
		case 4:
			appendConsoleColor(s, COLOR_WHITE);
			ch = '.'; break;
		default:
			appendConsoleColor(s, COLOR_WHITE);
			break;
	}
	s.push_back(ch);
	appendConsoleColor(s, COLOR_WHITE);
}