NS_IMETHODIMP nsGNOMEShellService::GetDesktopBackgroundColor(PRUint32 *aColor) { nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID); nsCAutoString background; gconf->GetString(NS_LITERAL_CSTRING(kDesktopColorKey), background); if (background.IsEmpty()) { *aColor = 0; return NS_OK; } // Chop off the leading '#' character background.Cut(0, 1); nscolor rgb; if (!NS_ASCIIHexToRGB(background, &rgb)) return NS_ERROR_FAILURE; // The result must be in RGB order with the high 8 bits zero. *aColor = (NS_GET_R(rgb) << 16 | NS_GET_G(rgb) << 8 | NS_GET_B(rgb)); return NS_OK; }
int main(int argc, char** argv) { ScopedXPCOM xpcom("TestColorNames"); if (xpcom.failed()) return 1; nscolor rgb; int rv = 0; // First make sure we can find all of the tags that are supposed to // be in the table. Futz with the case to make sure any case will // work for (PRUint32 index = 0 ; index < ArrayLength(kColorNames); index++) { // Lookup color by name and make sure it has the right id nsCString tagName(kColorNames[index]); // Check that color lookup by name gets the right rgb value if (!NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tagName), &rgb)) { fail("can't find '%s'", tagName.get()); rv = 1; } if (rgb != kColors[index]) { fail("name='%s' ColorNameToRGB=%x kColors[%d]=%08x", tagName.get(), rgb, index, kColors[index]); rv = 1; } // fiddle with the case to make sure we can still find it tagName.SetCharAt(tagName.CharAt(0) - 32, 0); if (!NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tagName), &rgb)) { fail("can't find '%s'", tagName.get()); rv = 1; } if (rgb != kColors[index]) { fail("name='%s' ColorNameToRGB=%x kColors[%d]=%08x", tagName.get(), rgb, index, kColors[index]); rv = 1; } // Check that parsing an RGB value in hex gets the right values PRUint8 r = NS_GET_R(rgb); PRUint8 g = NS_GET_G(rgb); PRUint8 b = NS_GET_B(rgb); PRUint8 a = NS_GET_A(rgb); if (a != PR_UINT8_MAX) { // NS_HexToRGB() can not handle a color with alpha channel rgb = NS_RGB(r, g, b); } char cbuf[50]; PR_snprintf(cbuf, sizeof(cbuf), "%02x%02x%02x", r, g, b); nscolor hexrgb; if (!NS_HexToRGB(NS_ConvertASCIItoUTF16(cbuf), &hexrgb)) { fail("hex conversion to color of '%s'", cbuf); rv = 1; } if (hexrgb != rgb) { fail("rgb=%x hexrgb=%x", rgb, hexrgb); rv = 1; } } // Now make sure we don't find some garbage for (PRUint32 i = 0; i < ArrayLength(kJunkNames); i++) { nsCString tag(kJunkNames[i]); if (NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tag), &rgb)) { fail("found '%s'", kJunkNames[i] ? kJunkNames[i] : "(null)"); rv = 1; } } if (rv == 0) passed("TestColorNames"); return rv; }
void nsCSSValue::AppendToString(nsCSSProperty aProperty, nsAString& aResult) const { // eCSSProperty_UNKNOWN gets used for some recursive calls below. NS_ABORT_IF_FALSE((0 <= aProperty && aProperty <= eCSSProperty_COUNT_no_shorthands) || aProperty == eCSSProperty_UNKNOWN, "property ID out of range"); nsCSSUnit unit = GetUnit(); if (unit == eCSSUnit_Null) { return; } if (eCSSUnit_String <= unit && unit <= eCSSUnit_Attr) { if (unit == eCSSUnit_Attr) { aResult.AppendLiteral("attr("); } nsAutoString buffer; GetStringValue(buffer); if (unit == eCSSUnit_String) { nsStyleUtil::AppendEscapedCSSString(buffer, aResult); } else if (unit == eCSSUnit_Families) { // XXX We really need to do *some* escaping. aResult.Append(buffer); } else { nsStyleUtil::AppendEscapedCSSIdent(buffer, aResult); } } else if (eCSSUnit_Array <= unit && unit <= eCSSUnit_Steps) { switch (unit) { case eCSSUnit_Counter: aResult.AppendLiteral("counter("); break; case eCSSUnit_Counters: aResult.AppendLiteral("counters("); break; case eCSSUnit_Cubic_Bezier: aResult.AppendLiteral("cubic-bezier("); break; case eCSSUnit_Steps: aResult.AppendLiteral("steps("); break; default: break; } nsCSSValue::Array *array = GetArrayValue(); bool mark = false; for (size_t i = 0, i_end = array->Count(); i < i_end; ++i) { if (mark && array->Item(i).GetUnit() != eCSSUnit_Null) { if (unit == eCSSUnit_Array && eCSSProperty_transition_timing_function != aProperty) aResult.AppendLiteral(" "); else aResult.AppendLiteral(", "); } if (unit == eCSSUnit_Steps && i == 1) { NS_ABORT_IF_FALSE(array->Item(i).GetUnit() == eCSSUnit_Enumerated && (array->Item(i).GetIntValue() == NS_STYLE_TRANSITION_TIMING_FUNCTION_STEP_START || array->Item(i).GetIntValue() == NS_STYLE_TRANSITION_TIMING_FUNCTION_STEP_END), "unexpected value"); if (array->Item(i).GetIntValue() == NS_STYLE_TRANSITION_TIMING_FUNCTION_STEP_START) { aResult.AppendLiteral("start"); } else { aResult.AppendLiteral("end"); } continue; } nsCSSProperty prop = ((eCSSUnit_Counter <= unit && unit <= eCSSUnit_Counters) && i == array->Count() - 1) ? eCSSProperty_list_style_type : aProperty; if (array->Item(i).GetUnit() != eCSSUnit_Null) { array->Item(i).AppendToString(prop, aResult); mark = true; } } if (eCSSUnit_Array == unit && aProperty == eCSSProperty_transition_timing_function) { aResult.AppendLiteral(")"); } } /* Although Function is backed by an Array, we'll handle it separately * because it's a bit quirky. */ else if (eCSSUnit_Function == unit) { const nsCSSValue::Array* array = GetArrayValue(); NS_ABORT_IF_FALSE(array->Count() >= 1, "Functions must have at least one element for the name."); /* Append the function name. */ const nsCSSValue& functionName = array->Item(0); if (functionName.GetUnit() == eCSSUnit_Enumerated) { // We assume that the first argument is always of nsCSSKeyword type. const nsCSSKeyword functionId = static_cast<nsCSSKeyword>(functionName.GetIntValue()); nsStyleUtil::AppendEscapedCSSIdent( NS_ConvertASCIItoUTF16(nsCSSKeywords::GetStringValue(functionId)), aResult); } else { functionName.AppendToString(aProperty, aResult); } aResult.AppendLiteral("("); /* Now, step through the function contents, writing each of them as we go. */ for (size_t index = 1; index < array->Count(); ++index) { array->Item(index).AppendToString(aProperty, aResult); /* If we're not at the final element, append a comma. */ if (index + 1 != array->Count()) aResult.AppendLiteral(", "); } /* Finally, append the closing parenthesis. */ aResult.AppendLiteral(")"); } else if (IsCalcUnit()) { NS_ABORT_IF_FALSE(GetUnit() == eCSSUnit_Calc, "unexpected unit"); CSSValueSerializeCalcOps ops(aProperty, aResult); css::SerializeCalc(*this, ops); } else if (eCSSUnit_Integer == unit) { aResult.AppendInt(GetIntValue(), 10); } else if (eCSSUnit_Enumerated == unit) { if (eCSSProperty_text_decoration_line == aProperty) { PRInt32 intValue = GetIntValue(); if (NS_STYLE_TEXT_DECORATION_LINE_NONE == intValue) { AppendASCIItoUTF16(nsCSSProps::LookupPropertyValue(aProperty, intValue), aResult); } else { // Ignore the "override all" internal value. // (It doesn't have a string representation.) intValue &= ~NS_STYLE_TEXT_DECORATION_LINE_OVERRIDE_ALL; nsStyleUtil::AppendBitmaskCSSValue( aProperty, intValue, NS_STYLE_TEXT_DECORATION_LINE_UNDERLINE, NS_STYLE_TEXT_DECORATION_LINE_PREF_ANCHORS, aResult); } } else if (eCSSProperty_marks == aProperty) { PRInt32 intValue = GetIntValue(); if (intValue == NS_STYLE_PAGE_MARKS_NONE) { AppendASCIItoUTF16(nsCSSProps::LookupPropertyValue(aProperty, intValue), aResult); } else { nsStyleUtil::AppendBitmaskCSSValue(aProperty, intValue, NS_STYLE_PAGE_MARKS_CROP, NS_STYLE_PAGE_MARKS_REGISTER, aResult); } } else if (eCSSProperty_unicode_bidi == aProperty) { PR_STATIC_ASSERT(NS_STYLE_UNICODE_BIDI_NORMAL == 0); PRInt32 intValue = GetIntValue(); if (NS_STYLE_UNICODE_BIDI_NORMAL == intValue) { AppendASCIItoUTF16(nsCSSProps::LookupPropertyValue(aProperty, intValue), aResult); } else { nsStyleUtil::AppendBitmaskCSSValue( aProperty, intValue, NS_STYLE_UNICODE_BIDI_EMBED, NS_STYLE_UNICODE_BIDI_PLAINTEXT, aResult); } } else { const nsAFlatCString& name = nsCSSProps::LookupPropertyValue(aProperty, GetIntValue()); AppendASCIItoUTF16(name, aResult); } } else if (eCSSUnit_EnumColor == unit) { // we can lookup the property in the ColorTable and then // get a string mapping the name nsCAutoString str; if (nsCSSProps::GetColorName(GetIntValue(), str)){ AppendASCIItoUTF16(str, aResult); } else { NS_ABORT_IF_FALSE(false, "bad color value"); } } else if (eCSSUnit_Color == unit) { nscolor color = GetColorValue(); if (color == NS_RGBA(0, 0, 0, 0)) { // Use the strictest match for 'transparent' so we do correct // round-tripping of all other rgba() values. aResult.AppendLiteral("transparent"); } else { PRUint8 a = NS_GET_A(color); if (a < 255) { aResult.AppendLiteral("rgba("); } else { aResult.AppendLiteral("rgb("); } NS_NAMED_LITERAL_STRING(comma, ", "); aResult.AppendInt(NS_GET_R(color), 10); aResult.Append(comma); aResult.AppendInt(NS_GET_G(color), 10); aResult.Append(comma); aResult.AppendInt(NS_GET_B(color), 10); if (a < 255) { aResult.Append(comma); aResult.AppendFloat(nsStyleUtil::ColorComponentToFloat(a)); } aResult.Append(PRUnichar(')')); } } else if (eCSSUnit_URL == unit || eCSSUnit_Image == unit) { aResult.Append(NS_LITERAL_STRING("url(")); nsStyleUtil::AppendEscapedCSSString( nsDependentString(GetOriginalURLValue()), aResult); aResult.Append(NS_LITERAL_STRING(")")); } else if (eCSSUnit_Element == unit) { aResult.Append(NS_LITERAL_STRING("-moz-element(#")); nsAutoString tmpStr; GetStringValue(tmpStr); nsStyleUtil::AppendEscapedCSSIdent(tmpStr, aResult); aResult.Append(NS_LITERAL_STRING(")")); } else if (eCSSUnit_Percent == unit) { aResult.AppendFloat(GetPercentValue() * 100.0f); } else if (eCSSUnit_Percent < unit) { // length unit aResult.AppendFloat(GetFloatValue()); } else if (eCSSUnit_Gradient == unit) { nsCSSValueGradient* gradient = GetGradientValue(); if (gradient->mIsRepeating) { if (gradient->mIsRadial) aResult.AppendLiteral("-moz-repeating-radial-gradient("); else aResult.AppendLiteral("-moz-repeating-linear-gradient("); } else { if (gradient->mIsRadial) aResult.AppendLiteral("-moz-radial-gradient("); else aResult.AppendLiteral("-moz-linear-gradient("); } if (gradient->mIsToCorner) { aResult.AppendLiteral("to"); NS_ABORT_IF_FALSE(gradient->mBgPos.mXValue.GetUnit() == eCSSUnit_Enumerated && gradient->mBgPos.mYValue.GetUnit() == eCSSUnit_Enumerated, "unexpected unit"); if (!(gradient->mBgPos.mXValue.GetIntValue() & NS_STYLE_BG_POSITION_CENTER)) { aResult.AppendLiteral(" "); gradient->mBgPos.mXValue.AppendToString(eCSSProperty_background_position, aResult); } if (!(gradient->mBgPos.mYValue.GetIntValue() & NS_STYLE_BG_POSITION_CENTER)) { aResult.AppendLiteral(" "); gradient->mBgPos.mYValue.AppendToString(eCSSProperty_background_position, aResult); } aResult.AppendLiteral(", "); } else if (gradient->mBgPos.mXValue.GetUnit() != eCSSUnit_None || gradient->mBgPos.mYValue.GetUnit() != eCSSUnit_None || gradient->mAngle.GetUnit() != eCSSUnit_None) { if (gradient->mBgPos.mXValue.GetUnit() != eCSSUnit_None) { gradient->mBgPos.mXValue.AppendToString(eCSSProperty_background_position, aResult); aResult.AppendLiteral(" "); } if (gradient->mBgPos.mXValue.GetUnit() != eCSSUnit_None) { gradient->mBgPos.mYValue.AppendToString(eCSSProperty_background_position, aResult); aResult.AppendLiteral(" "); } if (gradient->mAngle.GetUnit() != eCSSUnit_None) { gradient->mAngle.AppendToString(aProperty, aResult); } aResult.AppendLiteral(", "); } if (gradient->mIsRadial && (gradient->mRadialShape.GetUnit() != eCSSUnit_None || gradient->mRadialSize.GetUnit() != eCSSUnit_None)) { if (gradient->mRadialShape.GetUnit() != eCSSUnit_None) { NS_ABORT_IF_FALSE(gradient->mRadialShape.GetUnit() == eCSSUnit_Enumerated, "bad unit for radial gradient shape"); PRInt32 intValue = gradient->mRadialShape.GetIntValue(); NS_ABORT_IF_FALSE(intValue != NS_STYLE_GRADIENT_SHAPE_LINEAR, "radial gradient with linear shape?!"); AppendASCIItoUTF16(nsCSSProps::ValueToKeyword(intValue, nsCSSProps::kRadialGradientShapeKTable), aResult); aResult.AppendLiteral(" "); } if (gradient->mRadialSize.GetUnit() != eCSSUnit_None) { NS_ABORT_IF_FALSE(gradient->mRadialSize.GetUnit() == eCSSUnit_Enumerated, "bad unit for radial gradient size"); PRInt32 intValue = gradient->mRadialSize.GetIntValue(); AppendASCIItoUTF16(nsCSSProps::ValueToKeyword(intValue, nsCSSProps::kRadialGradientSizeKTable), aResult); } aResult.AppendLiteral(", "); } for (PRUint32 i = 0 ;;) { gradient->mStops[i].mColor.AppendToString(aProperty, aResult); if (gradient->mStops[i].mLocation.GetUnit() != eCSSUnit_None) { aResult.AppendLiteral(" "); gradient->mStops[i].mLocation.AppendToString(aProperty, aResult); } if (++i == gradient->mStops.Length()) { break; } aResult.AppendLiteral(", "); } aResult.AppendLiteral(")"); } else if (eCSSUnit_Pair == unit) { GetPairValue().AppendToString(aProperty, aResult); } else if (eCSSUnit_Triplet == unit) { GetTripletValue().AppendToString(aProperty, aResult); } else if (eCSSUnit_Rect == unit) { GetRectValue().AppendToString(aProperty, aResult); } else if (eCSSUnit_List == unit || eCSSUnit_ListDep == unit) { GetListValue()->AppendToString(aProperty, aResult); } else if (eCSSUnit_PairList == unit || eCSSUnit_PairListDep == unit) { GetPairListValue()->AppendToString(aProperty, aResult); } switch (unit) { case eCSSUnit_Null: break; case eCSSUnit_Auto: aResult.AppendLiteral("auto"); break; case eCSSUnit_Inherit: aResult.AppendLiteral("inherit"); break; case eCSSUnit_Initial: aResult.AppendLiteral("-moz-initial"); break; case eCSSUnit_None: aResult.AppendLiteral("none"); break; case eCSSUnit_Normal: aResult.AppendLiteral("normal"); break; case eCSSUnit_System_Font: aResult.AppendLiteral("-moz-use-system-font"); break; case eCSSUnit_All: aResult.AppendLiteral("all"); break; case eCSSUnit_Dummy: case eCSSUnit_DummyInherit: NS_ABORT_IF_FALSE(false, "should never serialize"); break; case eCSSUnit_String: break; case eCSSUnit_Ident: break; case eCSSUnit_Families: break; case eCSSUnit_URL: break; case eCSSUnit_Image: break; case eCSSUnit_Element: break; case eCSSUnit_Array: break; case eCSSUnit_Attr: case eCSSUnit_Cubic_Bezier: case eCSSUnit_Steps: case eCSSUnit_Counter: case eCSSUnit_Counters: aResult.Append(PRUnichar(')')); break; case eCSSUnit_Local_Font: break; case eCSSUnit_Font_Format: break; case eCSSUnit_Function: break; case eCSSUnit_Calc: break; case eCSSUnit_Calc_Plus: break; case eCSSUnit_Calc_Minus: break; case eCSSUnit_Calc_Times_L: break; case eCSSUnit_Calc_Times_R: break; case eCSSUnit_Calc_Divided: break; case eCSSUnit_Integer: break; case eCSSUnit_Enumerated: break; case eCSSUnit_EnumColor: break; case eCSSUnit_Color: break; case eCSSUnit_Percent: aResult.Append(PRUnichar('%')); break; case eCSSUnit_Number: break; case eCSSUnit_Gradient: break; case eCSSUnit_Pair: break; case eCSSUnit_Triplet: break; case eCSSUnit_Rect: break; case eCSSUnit_List: break; case eCSSUnit_ListDep: break; case eCSSUnit_PairList: break; case eCSSUnit_PairListDep: break; case eCSSUnit_Inch: aResult.AppendLiteral("in"); break; case eCSSUnit_Millimeter: aResult.AppendLiteral("mm"); break; case eCSSUnit_PhysicalMillimeter: aResult.AppendLiteral("mozmm"); break; case eCSSUnit_Centimeter: aResult.AppendLiteral("cm"); break; case eCSSUnit_Point: aResult.AppendLiteral("pt"); break; case eCSSUnit_Pica: aResult.AppendLiteral("pc"); break; case eCSSUnit_EM: aResult.AppendLiteral("em"); break; case eCSSUnit_XHeight: aResult.AppendLiteral("ex"); break; case eCSSUnit_Char: aResult.AppendLiteral("ch"); break; case eCSSUnit_RootEM: aResult.AppendLiteral("rem"); break; case eCSSUnit_Pixel: aResult.AppendLiteral("px"); break; case eCSSUnit_Degree: aResult.AppendLiteral("deg"); break; case eCSSUnit_Grad: aResult.AppendLiteral("grad"); break; case eCSSUnit_Radian: aResult.AppendLiteral("rad"); break; case eCSSUnit_Hertz: aResult.AppendLiteral("Hz"); break; case eCSSUnit_Kilohertz: aResult.AppendLiteral("kHz"); break; case eCSSUnit_Seconds: aResult.Append(PRUnichar('s')); break; case eCSSUnit_Milliseconds: aResult.AppendLiteral("ms"); break; } }
//------------------------------------------------------------------------ ATSUTextLayout nsATSUIToolkit::GetTextLayout(short aFontNum, short aSize, PRBool aBold, PRBool aItalic, nscolor aColor) { ATSUTextLayout txLayout = nsnull; OSStatus err; if (nsATSUIUtils::gTxLayoutCache->Get(aFontNum, aSize, aBold, aItalic, aColor, &txLayout)) return txLayout; UniChar dmy[1]; err = ::ATSUCreateTextLayoutWithTextPtr (dmy, 0,0,0,0,NULL, NULL, &txLayout); if(noErr != err) { NS_WARNING("ATSUCreateTextLayoutWithTextPtr failed"); // goto errorDone; return nsnull; } ATSUStyle theStyle; err = ::ATSUCreateStyle(&theStyle); if(noErr != err) { NS_WARNING("ATSUCreateStyle failed"); // goto errorDoneDestroyTextLayout; err = ::ATSUDisposeTextLayout(txLayout); return nsnull; } ATSUAttributeTag theTag[ATTR_CNT]; ByteCount theValueSize[ATTR_CNT]; ATSUAttributeValuePtr theValue[ATTR_CNT]; //--- Font ID & Face ----- ATSUFontID atsuFontID; // The use of ATSUFONDtoFontID is not recommended, see // http://developer.apple.com/documentation/Carbon/Reference/ATSUI_Reference/atsu_reference_Reference/chapter_1.2_section_19.html FMFontStyle fbStyle; if (::FMGetFontFromFontFamilyInstance(aFontNum, 0, &atsuFontID, &fbStyle) == kFMInvalidFontErr) { NS_WARNING("FMGetFontFromFontFamilyInstance failed"); // goto errorDoneDestroyStyle; err = ::ATSUDisposeStyle(theStyle); err = ::ATSUDisposeTextLayout(txLayout); return nsnull; } theTag[0] = kATSUFontTag; theValueSize[0] = (ByteCount) sizeof(ATSUFontID); theValue[0] = (ATSUAttributeValuePtr) &atsuFontID; //--- Font ID & Face ----- //--- Size ----- float dev2app; short fontsize = aSize; dev2app = mContext->DevUnitsToAppUnits(); // Fixed size = FloatToFixed( roundf(float(fontsize) / dev2app)); Fixed size = FloatToFixed( (float) rint(float(fontsize) / dev2app)); if( FixRound ( size ) < 9 && !nsFontUtils::DisplayVerySmallFonts()) size = X2Fix(9); theTag[1] = kATSUSizeTag; theValueSize[1] = (ByteCount) sizeof(Fixed); theValue[1] = (ATSUAttributeValuePtr) &size; //--- Size ----- //--- Color ----- RGBColor color; #define COLOR8TOCOLOR16(color8) ((color8 << 8) | color8) color.red = COLOR8TOCOLOR16(NS_GET_R(aColor)); color.green = COLOR8TOCOLOR16(NS_GET_G(aColor)); color.blue = COLOR8TOCOLOR16(NS_GET_B(aColor)); theTag[2] = kATSUColorTag; theValueSize[2] = (ByteCount) sizeof(RGBColor); theValue[2] = (ATSUAttributeValuePtr) &color; //--- Color ----- //--- Bold ----- Boolean isBold = aBold ? true : false; theTag[3] = kATSUQDBoldfaceTag; theValueSize[3] = (ByteCount) sizeof(Boolean); theValue[3] = (ATSUAttributeValuePtr) &isBold; //--- Bold ----- //--- Italic ----- Boolean isItalic = aItalic ? true : false; theTag[4] = kATSUQDItalicTag; theValueSize[4] = (ByteCount) sizeof(Boolean); theValue[4] = (ATSUAttributeValuePtr) &isItalic; //--- Italic ----- err = ::ATSUSetAttributes(theStyle, ATTR_CNT, theTag, theValueSize, theValue); if(noErr != err) { NS_WARNING("ATSUSetAttributes failed"); // goto errorDoneDestroyStyle; err = ::ATSUDisposeStyle(theStyle); err = ::ATSUDisposeTextLayout(txLayout); return nsnull; } err = ::ATSUSetRunStyle(txLayout, theStyle, kATSUFromTextBeginning, kATSUToTextEnd); if(noErr != err) { NS_WARNING("ATSUSetRunStyle failed"); // goto errorDoneDestroyStyle; err = ::ATSUDisposeStyle(theStyle); err = ::ATSUDisposeTextLayout(txLayout); return nsnull; } err = ::ATSUSetTransientFontMatching(txLayout, true); if(noErr != err) { NS_WARNING( "ATSUSetTransientFontMatching failed"); // goto errorDoneDestroyStyle; err = ::ATSUDisposeStyle(theStyle); err = ::ATSUDisposeTextLayout(txLayout); return nsnull; } nsATSUIUtils::gTxLayoutCache->Set(aFontNum, aSize, aBold, aItalic, aColor, txLayout); return txLayout; }
extern "C" NS_GFX_(nscolor) NS_BrightenColor(nscolor inColor) { PRIntn r, g, b, max, over; r = NS_GET_R(inColor); g = NS_GET_G(inColor); b = NS_GET_B(inColor); //10% of max color increase across the board r += 25; g += 25; b += 25; //figure out which color is largest if (r > g) { if (b > r) max = b; else max = r; } else { if (b > g) max = b; else max = g; } //if we overflowed on this max color, increase //other components by the overflow amount if (max > 255) { over = max - 255; if (max == r) { g += over; b += over; } else if (max == g) { r += over; b += over; } else { r += over; g += over; } } //clamp if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255; return NS_RGBA(r, g, b, NS_GET_A(inColor)); }
// // All these routines will return NS_OK if they have a value, // in which case the nsLookAndFeel should use that value; // otherwise we'll return NS_ERROR_NOT_AVAILABLE, in which case, the // platform-specific nsLookAndFeel should use its own values instead. // NS_IMETHODIMP nsXPLookAndFeel::GetColor(const nsColorID aID, nscolor &aColor) { if (!sInitialized) Init(); // define DEBUG_SYSTEM_COLOR_USE if you want to debug system color // use in a skin that uses them. When set, it will make all system // color pairs that are appropriate for foreground/background // pairing the same. This means if the skin is using system colors // correctly you will not be able to see *any* text. #undef DEBUG_SYSTEM_COLOR_USE #ifdef DEBUG_SYSTEM_COLOR_USE { nsresult rv = NS_OK; switch (aID) { // css2 http://www.w3.org/TR/REC-CSS2/ui.html#system-colors case eColor_activecaption: // active window caption background case eColor_captiontext: // text in active window caption aColor = NS_RGB(0xff, 0x00, 0x00); break; case eColor_highlight: // background of selected item case eColor_highlighttext: // text of selected item aColor = NS_RGB(0xff, 0xff, 0x00); break; case eColor_inactivecaption: // inactive window caption case eColor_inactivecaptiontext: // text in inactive window caption aColor = NS_RGB(0x66, 0x66, 0x00); break; case eColor_infobackground: // tooltip background color case eColor_infotext: // tooltip text color aColor = NS_RGB(0x00, 0xff, 0x00); break; case eColor_menu: // menu background case eColor_menutext: // menu text aColor = NS_RGB(0x00, 0xff, 0xff); break; case eColor_threedface: case eColor_buttonface: // 3-D face color case eColor_buttontext: // text on push buttons aColor = NS_RGB(0x00, 0x66, 0x66); break; case eColor_window: case eColor_windowtext: aColor = NS_RGB(0x00, 0x00, 0xff); break; // from the CSS3 working draft (not yet finalized) // http://www.w3.org/tr/2000/wd-css3-userint-20000216.html#color case eColor__moz_field: case eColor__moz_fieldtext: aColor = NS_RGB(0xff, 0x00, 0xff); break; case eColor__moz_dialog: case eColor__moz_dialogtext: aColor = NS_RGB(0x66, 0x00, 0x66); break; default: rv = NS_ERROR_NOT_AVAILABLE; } if (NS_SUCCEEDED(rv)) return rv; } #endif // DEBUG_SYSTEM_COLOR_USE if (IS_COLOR_CACHED(aID)) { aColor = sCachedColors[aID]; return NS_OK; } // There are no system color settings for these, so set them manually if (aID == eColor_TextSelectBackgroundDisabled) { // This is used to gray out the selection when it's not focused // Used with nsISelectionController::SELECTION_DISABLED aColor = NS_RGB(0xb0, 0xb0, 0xb0); return NS_OK; } if (aID == eColor_TextSelectBackgroundAttention) { // This makes the selection stand out when typeaheadfind is on // Used with nsISelectionController::SELECTION_ATTENTION aColor = NS_RGB(0x38, 0xd8, 0x78); return NS_OK; } if (NS_SUCCEEDED(NativeGetColor(aID, aColor))) { if (gfxPlatform::IsCMSEnabled() && !IsSpecialColor(aID, aColor)) { cmsHTRANSFORM transform = gfxPlatform::GetCMSInverseRGBTransform(); if (transform) { PRUint8 color[3]; color[0] = NS_GET_R(aColor); color[1] = NS_GET_G(aColor); color[2] = NS_GET_B(aColor); cmsDoTransform(transform, color, color, 1); aColor = NS_RGB(color[0], color[1], color[2]); } } CACHE_COLOR(aID, aColor); return NS_OK; } return NS_ERROR_NOT_AVAILABLE; }
PRBool nsSVGGradientFrame::SetupPaintServer(gfxContext *aContext, nsSVGGeometryFrame *aSource, float aGraphicOpacity) { // Get the transform list (if there is one) gfxMatrix patternMatrix = GetGradientTransform(aSource); if (patternMatrix.IsSingular()) return PR_FALSE; PRUint32 nStops = GetStopCount(); // SVG specification says that no stops should be treated like // the corresponding fill or stroke had "none" specified. if (nStops == 0) { aContext->SetColor(gfxRGBA(0, 0, 0, 0)); return PR_TRUE; } patternMatrix.Invert(); nsRefPtr<gfxPattern> gradient = CreateGradient(); if (!gradient || gradient->CairoStatus()) return PR_FALSE; PRUint16 aSpread = GetSpreadMethod(); if (aSpread == nsIDOMSVGGradientElement::SVG_SPREADMETHOD_PAD) gradient->SetExtend(gfxPattern::EXTEND_PAD); else if (aSpread == nsIDOMSVGGradientElement::SVG_SPREADMETHOD_REFLECT) gradient->SetExtend(gfxPattern::EXTEND_REFLECT); else if (aSpread == nsIDOMSVGGradientElement::SVG_SPREADMETHOD_REPEAT) gradient->SetExtend(gfxPattern::EXTEND_REPEAT); gradient->SetMatrix(patternMatrix); // setup stops float lastOffset = 0.0f; for (PRUint32 i = 0; i < nStops; i++) { float offset, stopOpacity; nscolor stopColor; GetStopInformation(i, &offset, &stopColor, &stopOpacity); if (offset < lastOffset) offset = lastOffset; else lastOffset = offset; gradient->AddColorStop(offset, gfxRGBA(NS_GET_R(stopColor)/255.0, NS_GET_G(stopColor)/255.0, NS_GET_B(stopColor)/255.0, NS_GET_A(stopColor)/255.0 * stopOpacity * aGraphicOpacity)); } aContext->SetPattern(gradient); return PR_TRUE; }
void NS_GetSpecial3DColors(nscolor aResult[2], nscolor aBackgroundColor, nscolor aBorderColor) { uint8_t f0, f1; uint8_t r, g, b; uint8_t rb = NS_GET_R(aBorderColor); uint8_t gb = NS_GET_G(aBorderColor); uint8_t bb = NS_GET_B(aBorderColor); uint8_t a = NS_GET_A(aBorderColor); // This needs to be optimized. // Calculating background brightness again and again is // a waste of time!!!. Just calculate it only once. // .....somehow!!! uint8_t red = NS_GET_R(aBackgroundColor); uint8_t green = NS_GET_G(aBackgroundColor); uint8_t blue = NS_GET_B(aBackgroundColor); uint8_t elementBrightness = NS_GetBrightness(rb,gb,bb); uint8_t backgroundBrightness = NS_GetBrightness(red, green, blue); if (backgroundBrightness < COLOR_DARK_THRESHOLD) { f0 = COLOR_DARK_BS_FACTOR; f1 = COLOR_DARK_TS_FACTOR; if(elementBrightness == MAX_DARKNESS) { rb = NS_GET_R(DARK_GRAY); gb = NS_GET_G(DARK_GRAY); bb = NS_GET_B(DARK_GRAY); } }else if (backgroundBrightness > COLOR_LIGHT_THRESHOLD) { f0 = COLOR_LITE_BS_FACTOR; f1 = COLOR_LITE_TS_FACTOR; if(elementBrightness == MAX_BRIGHTNESS) { rb = NS_GET_R(LIGHT_GRAY); gb = NS_GET_G(LIGHT_GRAY); bb = NS_GET_B(LIGHT_GRAY); } }else { f0 = COLOR_DARK_BS_FACTOR + (backgroundBrightness * (COLOR_LITE_BS_FACTOR - COLOR_DARK_BS_FACTOR) / MAX_COLOR); f1 = COLOR_DARK_TS_FACTOR + (backgroundBrightness * (COLOR_LITE_TS_FACTOR - COLOR_DARK_TS_FACTOR) / MAX_COLOR); } r = rb - (f0 * rb / 100); g = gb - (f0 * gb / 100); b = bb - (f0 * bb / 100); aResult[0] = NS_RGBA(r, g, b, a); r = rb + (f1 * (MAX_COLOR - rb) / 100); g = gb + (f1 * (MAX_COLOR - gb) / 100); b = bb + (f1 * (MAX_COLOR - bb) / 100); aResult[1] = NS_RGBA(r, g, b, a); }