Re: [css3-color] #rrggbbaa annotation

In order to prove that its implementation is very easy, I downloaded
the Firefox source code so I could show you more or less which are the
necessary changes:

File: layout/style/nsCSSParser.cpp @ Line: 3564  (No changes required)

CSSParserImpl::ParseColor(....
...
    case eCSSToken_Ref:
      // #xxyyzz
      if (NS_HexToRGB(tk->mIdent, &rgba)) {
        aValue.SetColorValue(rgba);
        return PR_TRUE;
      }
      break;

As you can see in this file, Hex values are converted into RGB... But
why they are using rgba here?? Because it is capable but not
implemented... Let's see more in detail:

File: gfx/src/nsColor.cpp @ Line: 122 (2 lines added, 2 modified)

NS_HexToRGB(...

  int nameLen = aColorSpec.Length();
  if ((nameLen == 3) || (nameLen == 6)) {  <--- extend this line to
include: (nameLen == 4 || nameLen == 8)
    // Make sure the digits are legal
    for (int i = 0; i < nameLen; i++) {
         ....
    }

    // Convert the ascii to binary
    int dpc = ((3 == nameLen) ? 1 : 2);  <-- modify this line for
(nameLen = 4) = 3 and (nameLen = 8) = 4
    // Translate components from hex to binary
    int r = ComponentValue(buffer, nameLen, 0, dpc);
    int g = ComponentValue(buffer, nameLen, 1, dpc);
    int b = ComponentValue(buffer, nameLen, 2, dpc);
    int a = ComponentValue(buffer, nameLen, 3, dpc); <---- this line
to be added (add and if ... )
    if (dpc == 1) {
      // Scale single digit component to an 8 bit value. Replicate the
      // single digit to compute the new value.
      r = (r << 4) | r;
      g = (g << 4) | g;
      b = (b << 4) | b;
      a = (a << 4) | a; <-- this line to be added
    }
    NS_ASSERTION((r >= 0) && (r <= 255), "bad r");
    NS_ASSERTION((g >= 0) && (g <= 255), "bad g");
    NS_ASSERTION((b >= 0) && (b <= 255), "bad b");
    NS_ASSERTION((a >= 0) && (a <= 255), "bad a");  <---- this line to
be added (add and if ... )
    if (nsnull != aResult) {
      *aResult = NS_RGB(r, g, b);  <--- change NS_RGB to NS_RGBA  (add
and if ... )
    }

At the end of this function, it calls to NS_RGB with 3 parameters.
There is a function called NS_RGBA with 4 parameters too!
So we just need to call that function instead and we are done.

These are in theory all the changes required in Firefox in order to
use #rrggbbaa. I spent no more than 30 minutes with it and I'm not a
Firefox developer (actually it's the first time that I have checked
its code) and I'm not proficient in C. I'm pretty sure they can beat
my time.

As the expected behavior of the additional #AA is very similar to the
previous #RRGGBB, IMHO there is no so much to discuss here. Just take
a look how Google defines it. No need to explain the math behind it.

Browser engines will not implement this feature unless it is
recommended (mainly Gecko,Webkit and Presto). The reason is very
simple: it would be almost impossible to use it with a custom prefix
(-moz, -webkit, etc) and there is no sense to implement it if no other
browsers will do it (as it is not in the specifications). That is the
reason it needs to start first here, in the css color module.

Received on Tuesday, 6 April 2010 14:53:16 UTC