color.cpp 1.54 KB
Newer Older
Nick Sharp's avatar
Nick Sharp committed
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
#include "color.h"

#include <stdio.h>
#include <string.h>
#include <ostream>
#include <sstream>

using namespace std;

namespace CMU462 {

// Constants
const Color Color::White  = Color(1,1,1,1);
const Color Color::Black  = Color(0,0,0,1);

Color::Color( const unsigned char* arr ) {
  float inv = 1.0 / 255.0;
  r = arr[0] * inv;
  g = arr[1] * inv;
  b = arr[2] * inv;
  a = 1.0;
}

Color Color::fromHex( const char* s ) {
  // If the color is "none", return any color
  // with alpha zero (completely transparent).
  if( !strcmp(s, "none") ) {
      return Color(0,0,0,0);
  }

  // Ignore leading hashmark.
  if( s[0] == '#' ) {
      s++;
  }

  // Set stream formatting to hexadecimal.
  stringstream ss;
  ss << hex;

  // Convert to integer.
  unsigned int rgb;
  ss << s;
  ss >> rgb;

  // Extract 8-byte chunks and normalize.
  Color c;
  c.r = (float)( ( rgb & 0xFF0000 ) >> 16 ) / 255.0;
  c.g = (float)( ( rgb & 0x00FF00 ) >>  8 ) / 255.0;
  c.b = (float)( ( rgb & 0x0000FF ) >>  0 ) / 255.0;
  c.a = 1.0; // set alpha to 1 (opaque) by default

  return c;
}

string Color::toHex( void ) const {
  int R = (unsigned char) max( 0., min( 255.0, 255.0 * r ));
  int G = (unsigned char) max( 0., min( 255.0, 255.0 * g ));
  int B = (unsigned char) max( 0., min( 255.0, 255.0 * b ));

  stringstream ss;
  ss << hex;

  ss << R << G << B;
  return ss.str();
}

std::ostream& operator<<( std::ostream& os, const Color& c ) {
  os << "(r=" << c.r;
  os << " g=" << c.g;
  os << " b=" << c.b;
  os << " a=" << c.a;
  os << ")";
  return os;
}

} // namespace CMU462