Changeset 21798 in webkit


Ignore:
Timestamp:
May 26, 2007 6:36:44 AM (17 years ago)
Author:
bdash
Message:

2007-05-26 Mitz Pettel <mitz@webkit.org>

Reviewed by Darin.

Not testable.

  • platform/graphics/Color.cpp: (WebCore::Color::light): (WebCore::Color::dark):
Location:
trunk/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r21797 r21798  
     12007-05-26  Mitz Pettel  <mitz@webkit.org>
     2
     3        Reviewed by Darin.
     4
     5        - http://bugs.webkit.org/show_bug.cgi?id=13842
     6          Simplify Color::light() and Color::dark()
     7
     8        Not testable.
     9
     10        * platform/graphics/Color.cpp:
     11        (WebCore::Color::light):
     12        (WebCore::Color::dark):
     13
    1142007-05-26  Mitz Pettel  <mitz@webkit.org>
    215
  • trunk/WebCore/platform/graphics/Color.cpp

    r20685 r21798  
    157157}
    158158
    159 const float undefinedHue = -1;
    160 
    161 // Explanations of these algorithms can be found at http://www.acm.org/jgt/papers/SmithLyons96/hsv_rgb.html
    162 static void convertRGBToHSV(float r, float g, float b, float& h, float& s, float& v)
    163 {
    164     float x = min(r, min(g, b));
    165     v = max(r, max(g, b));
    166 
    167     if (v == x) {
    168         h = undefinedHue;
    169         s = 0;
    170     } else {
    171         float f = (r == x) ? g - b : ((g == x) ? b - r : r - g);
    172         int i = (r == x) ? 3 : ((g == x) ? 5 : 1);
    173         h = i - f / (v - x);
    174         if (v != 0)
    175             s = (v - x) / v;
    176         else
    177             s = 0;
    178     }
    179 }
    180 
    181 static void convertHSVToRGB(float h, float s, float v, float& r, float& g, float& b)
    182 {
    183     if (h == undefinedHue) {
    184         r = v;
    185         g = v;
    186         b = v;
    187         return;
    188     }
    189 
    190     int i = static_cast<int>(h); // sector 0 to 5
    191     float f = h - i; // fractional part of h
    192     if (!(i & 1))
    193         f = 1 - f;
    194     float m = v * (1 - s);
    195     float n = v * (1 - s * f);
    196 
    197     switch (i) {
    198         default: // 0 and 6
    199             r = v;
    200             g = n;
    201             b = m;
    202             break;
    203         case 1:
    204             r = n;
    205             g = v;
    206             b = m;
    207             break;
    208         case 2:
    209             r = m;
    210             g = v;
    211             b = n;
    212             break;
    213         case 3:
    214             r = m;
    215             g = n;
    216             b = v;
    217             break;
    218         case 4:
    219             r = n;
    220             g = m;
    221             b = v;
    222             break;
    223         case 5:
    224             r = v;
    225             g = m;
    226             b = n;
    227             break;
    228     }
    229 }
    230 
    231159Color Color::light() const
    232160{
     
    237165    const float scaleFactor = nextafterf(256.0f, 0.0f);
    238166
    239     float r, g, b, a, h, s, v;
     167    float r, g, b, a;
    240168    getRGBA(r, g, b, a);
    241     convertRGBToHSV(r, g, b, h, s, v);
    242     v = max(0.0f, min(v + 0.33f, 1.0f));
    243     convertHSVToRGB(h, s, v, r, g, b);
    244     return Color(static_cast<int>(r * scaleFactor),
    245                  static_cast<int>(g * scaleFactor),
    246                  static_cast<int>(b * scaleFactor),
    247                  static_cast<int>(a * scaleFactor));
     169
     170    float v = max(r, max(g, b));
     171
     172    if (v == 0.0f)
     173        // Lightened black with alpha.
     174        return Color(0x54, 0x54, 0x54, alpha());
     175
     176    float multiplier = min(1.0f, v + 0.33f) / v;
     177
     178    return Color(static_cast<int>(multiplier * r * scaleFactor),
     179                 static_cast<int>(multiplier * g * scaleFactor),
     180                 static_cast<int>(multiplier * b * scaleFactor),
     181                 alpha());
    248182}
    249183
     
    256190    const float scaleFactor = nextafterf(256.0f, 0.0f);
    257191
    258     float r, g, b, a, h, s, v;
     192    float r, g, b, a;
    259193    getRGBA(r, g, b, a);
    260     convertRGBToHSV(r, g, b, h, s, v);
    261     v = max(0.0f, min(v - 0.33f, 1.0f));
    262     convertHSVToRGB(h, s, v, r, g, b);
    263     return Color(static_cast<int>(r * scaleFactor),
    264                  static_cast<int>(g * scaleFactor),
    265                  static_cast<int>(b * scaleFactor),
    266                  static_cast<int>(a * scaleFactor));
     194
     195    float v = max(r, max(g, b));
     196    float multiplier = max(0.0f, (v - 0.33f) / v);
     197
     198    return Color(static_cast<int>(multiplier * r * scaleFactor),
     199                 static_cast<int>(multiplier * g * scaleFactor),
     200                 static_cast<int>(multiplier * b * scaleFactor),
     201                 alpha());
    267202}
    268203
Note: See TracChangeset for help on using the changeset viewer.