ColorSet.cc
上传用户:kellyonhid
上传日期:2013-10-12
资源大小:932k
文件大小:3k
源码类别:

3D图形编程

开发平台:

Visual C++

  1. //############################################################
  2. // ColorUtils.cc
  3. // Matt Ginzton / Kari Pulli
  4. // Mon Jun 22 13:39:49 PDT 1998
  5. // 
  6. // Utilities for dealing with colors
  7. //############################################################
  8. #include "ColorSet.h"
  9. ColorSet::ColorSet (void)
  10. {
  11.   currentColor = 0;
  12. }
  13. static float colorSet[][3] = {
  14.     {0.9*0.75, 0.15*0.75, 0.35*0.75},
  15.     {0.0*0.75, 0.9*0.75, 0.0*0.75},
  16.     {0.0*0.75, 0.9*0.75, 0.9*0.75},
  17.     {0.9*0.75, 0.0*0.75, 0.9*0.75},
  18.     {0.9*0.75, 0.9*0.75, 0.0*0.75},
  19.     {0.0*0.75, 0.0*0.75, 0.9*0.75},
  20.     {0.9*0.75, 0.5*0.75, 0.2*0.75},
  21.     {0.5*0.75, 0.9*0.75, 0.2*0.75},
  22.     {0.2*0.75, 0.5*0.75, 0.9*0.75},
  23.     {0.5*0.75, 0.2*0.75, 0.9*0.75},
  24.     {0.9*0.75, 0.2*0.75, 0.5*0.75},
  25.     {0.2*0.75, 0.9*0.75, 0.5*0.75},
  26.     {0.9*0.75, 0.7*0.75, 0.7*0.75},
  27.     {0.7*0.75, 0.9*0.75, 0.7*0.75},
  28.     {0.7*0.75, 0.7*0.75, 0.9*0.75},
  29.     {0.7*0.75, 0.9*0.75, 0.9*0.75},
  30.     {0.9*0.75, 0.7*0.75, 0.9*0.75},
  31.     {0.9*0.75, 0.9*0.75, 0.7*0.75},
  32.     {0.6*0.75, 0.6*0.75, 0.6*0.75},
  33.     {0.6*0.75, 0.3*0.75, 0.1*0.75},
  34.     {0.6*0.75, 0.1*0.75, 0.3*0.75},
  35.     {0.3*0.75, 0.1*0.75, 0.6*0.75},
  36.     {0.3*0.75, 0.6*0.75, 0.1*0.75},
  37.     {0.1*0.75, 0.3*0.75, 0.6*0.75},
  38.     {0.1*0.75, 0.6*0.75, 0.3*0.75},
  39.     {0.4*0.75, 0.9*0.75, 0.3*0.75},
  40.     {0.2*0.75, 0.9*0.75, 0.0*0.75},
  41. };
  42. #define NUM_COLOR_SET_COLORS (sizeof(colorSet) / sizeof(colorSet[0]))
  43. void
  44. ColorSet::chooseNewColor (uchar* color)
  45. {
  46.   currentColor = currentColor % NUM_COLOR_SET_COLORS;
  47.   
  48.   color[0] = uchar(colorSet[currentColor][0]*255);
  49.   color[1] = uchar(colorSet[currentColor][1]*255);
  50.   color[2] = uchar(colorSet[currentColor][2]*255);
  51.   
  52.   currentColor++;
  53. }
  54. void
  55. pushColor (vector<uchar>& colors, int colorsize, float value)
  56. {
  57.   pushColor (colors, colorsize, 255 * value);
  58. }
  59. void
  60. pushColor (vector<uchar>& colors, int colorsize, float* values)
  61. {
  62.   uchar cvalues[3] = { values[0] * 255, values[1] * 255, values[2] * 255 };
  63.   pushColor (colors, colorsize, cvalues);
  64. }
  65. void
  66. pushColor (vector<uchar>& colors, int colorsize,
  67.    float value1, float value2, float value3)
  68. {
  69.   uchar cvalues[3] = { value1 * 255, value2 * 255, value3 * 255 };
  70.   pushColor (colors, colorsize, cvalues);
  71. }
  72. void
  73. pushColor (vector<uchar>& colors, int colorsize, uchar value)
  74. {
  75.   switch (colorsize) {
  76.   case 1:
  77.   case 2:
  78.     colors.push_back (value);
  79.     break;
  80.   case 3:
  81.   case 4:
  82.     colors.push_back (value);
  83.     colors.push_back (value);
  84.     colors.push_back (value);
  85.     break;
  86.   }
  87.  
  88.   if (colorsize == 2 || colorsize == 4) {
  89.     // TODO: hardcoded alpha for now
  90.     colors.push_back (255);
  91.   }
  92. }
  93. void
  94. pushColor (vector<uchar>& colors, int colorsize, uchar* values)
  95. {
  96.   switch (colorsize) {
  97.   case 1:
  98.   case 2:
  99.     colors.push_back ((values[0] + values[1] + values[2]) / 3);
  100.     break;
  101.   case 3:
  102.   case 4:
  103.     colors.push_back (values[0]);
  104.     colors.push_back (values[1]);
  105.     colors.push_back (values[2]);
  106.     break;
  107.   }
  108.  
  109.   if (colorsize == 2 || colorsize == 4) {
  110.     // TODO: hardcoded alpha for now
  111.     colors.push_back (255);
  112.   }
  113. }
  114. void
  115. pushColor (vector<uchar>& colors, int colorsize,
  116.    uchar* values1, uchar* values2, uchar* values3)
  117. {
  118.   pushColor (colors, colorsize,
  119.      (values1[0] + values2[0] + values3[0]) / 3,
  120.      (values1[1] + values2[1] + values3[1]) / 3,
  121.      (values1[2] + values2[2] + values3[2]) / 3);
  122. }