extern "C" NS_GFX_(PRBool) NS_ASCIIHexToRGB(const nsCString& aColorSpec, nscolor* aResult) { const char* buffer = aColorSpec.get(); int nameLen = aColorSpec.Length(); if ((nameLen == 3) || (nameLen == 6)) { // Make sure the digits are legal for (int i = 0; i < nameLen; i++) { char ch = buffer[i]; if (((ch >= '0') && (ch <= '9')) || ((ch >= 'a') && (ch <= 'f')) || ((ch >= 'A') && (ch <= 'F'))) { // Legal character continue; } // Whoops. Illegal character. return PR_FALSE; } // Convert the ascii to binary int dpc = ((3 == nameLen) ? 1 : 2); // 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); 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; } NS_ASSERTION((r >= 0) && (r <= 255), "bad r"); NS_ASSERTION((g >= 0) && (g <= 255), "bad g"); NS_ASSERTION((b >= 0) && (b <= 255), "bad b"); if (nsnull != aResult) { *aResult = NS_RGB(r, g, b); } return PR_TRUE; } // Improperly formatted color value return PR_FALSE; }
bool HexToRGB(const std::string& aBuf, Color& aResult) { const char* buffer = aBuf.c_str(); int nameLen = aBuf.size(); if ((nameLen == 3) || (nameLen == 6)) { // Make sure the digits are legal for (int i = 0; i < nameLen; i++) { char ch = buffer[i]; if (((ch >= '0') && (ch <= '9')) || ((ch >= 'a') && (ch <= 'f')) || ((ch >= 'A') && (ch <= 'F'))) { // Legal character continue; } // Whoops. Illegal character. return false; } // Convert the ascii to binary int dpc = ((3 == nameLen) ? 1 : 2); // 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); 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; } assert((r >= 0) && (r <= 255)); assert((g >= 0) && (g <= 255)); assert((b >= 0) && (b <= 255)); aResult = ColorSetRGB(r, g, b); return true; } // Improperly formatted color value return false; }
// compatible with legacy Nav behavior extern "C" NS_GFX_(PRBool) NS_LooseHexToRGB(const nsString& aColorSpec, nscolor* aResult) { // XXXldb nsStackString<30> NS_LossyConvertUCS2toASCII buffer(aColorSpec); int nameLen = buffer.Length(); const char* colorSpec = buffer.get(); if ('#' == colorSpec[0]) { ++colorSpec; --nameLen; } if (3 < nameLen) { // Convert the ascii to binary int dpc = (nameLen / 3) + (((nameLen % 3) != 0) ? 1 : 0); if (4 < dpc) { dpc = 4; } // Translate components from hex to binary int r = ComponentValue(colorSpec, nameLen, 0, dpc); int g = ComponentValue(colorSpec, nameLen, 1, dpc); int b = ComponentValue(colorSpec, nameLen, 2, dpc); NS_ASSERTION((r >= 0) && (r <= 255), "bad r"); NS_ASSERTION((g >= 0) && (g <= 255), "bad g"); NS_ASSERTION((b >= 0) && (b <= 255), "bad b"); if (nsnull != aResult) { *aResult = NS_RGB(r, g, b); } } else { if (nsnull != aResult) { *aResult = NS_RGB(0, 0, 0); } } return PR_TRUE; }
// This implements part of the algorithm for legacy behavior described in // http://www.whatwg.org/specs/web-apps/current-work/complete/common-microsyntaxes.html#rules-for-parsing-a-legacy-color-value NS_GFX_(PRBool) NS_LooseHexToRGB(const nsString& aColorSpec, nscolor* aResult) { if (aColorSpec.EqualsLiteral("transparent")) { return PR_FALSE; } int nameLen = aColorSpec.Length(); const PRUnichar* colorSpec = aColorSpec.get(); if (nameLen > 128) { nameLen = 128; } if ('#' == colorSpec[0]) { ++colorSpec; --nameLen; } // digits per component int dpc = (nameLen + 2) / 3; int newdpc = dpc; // Use only the rightmost 8 characters of each component. if (newdpc > 8) { nameLen -= newdpc - 8; colorSpec += newdpc - 8; newdpc = 8; } // And then keep trimming characters at the left until we'd trim one // that would leave a nonzero value, but not past 2 characters per // component. while (newdpc > 2) { PRBool haveNonzero = PR_FALSE; for (int c = 0; c < 3; ++c) { NS_ABORT_IF_FALSE(c * dpc < nameLen, "should not pass end of string while newdpc > 2"); PRUnichar ch = colorSpec[c * dpc]; if (('1' <= ch && ch <= '9') || ('A' <= ch && ch <= 'F') || ('a' <= ch && ch <= 'f')) { haveNonzero = PR_TRUE; break; } } if (haveNonzero) { break; } --newdpc; --nameLen; ++colorSpec; } // Translate components from hex to binary int r = ComponentValue(colorSpec, nameLen, 0, dpc); int g = ComponentValue(colorSpec, nameLen, 1, dpc); int b = ComponentValue(colorSpec, nameLen, 2, dpc); NS_ASSERTION((r >= 0) && (r <= 255), "bad r"); NS_ASSERTION((g >= 0) && (g <= 255), "bad g"); NS_ASSERTION((b >= 0) && (b <= 255), "bad b"); *aResult = NS_RGB(r, g, b); return PR_TRUE; }