NS_ComposeColors(nscolor aBG, nscolor aFG) { // This function uses colors that are non premultiplied alpha. PRIntn r, g, b, a; PRIntn bgAlpha = NS_GET_A(aBG); PRIntn fgAlpha = NS_GET_A(aFG); // Compute the final alpha of the blended color // a = fgAlpha + bgAlpha*(255 - fgAlpha)/255; FAST_DIVIDE_BY_255(a, bgAlpha*(255-fgAlpha)); a = fgAlpha + a; PRIntn blendAlpha; if (a == 0) { // In this case the blended color is totally trasparent, // we preserve the color information of the foreground color. blendAlpha = 255; } else { blendAlpha = (fgAlpha*255)/a; } MOZ_BLEND(r, NS_GET_R(aBG), NS_GET_R(aFG), blendAlpha); MOZ_BLEND(g, NS_GET_G(aBG), NS_GET_G(aFG), blendAlpha); MOZ_BLEND(b, NS_GET_B(aBG), NS_GET_B(aFG), blendAlpha); return NS_RGBA(r, g, b, a); }
extern "C" NS_GFX_(nscolor) NS_DarkenColor(nscolor inColor) { PRIntn r, g, b, max; r = NS_GET_R(inColor); g = NS_GET_G(inColor); b = NS_GET_B(inColor); //10% of max color decrease 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 underflowed on this max color, decrease //other components by the underflow amount if (max < 0) { if (max == r) { g += max; b += max; } else if (max == g) { r += max; b += max; } else { r += max; g += max; } } //clamp if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0; return NS_RGBA(r, g, b, NS_GET_A(inColor)); }
extern "C" NS_GFX_(void) NS_RGBToHex(nscolor aColor, nsAString& aResult) { char buf[10]; PR_snprintf(buf, sizeof(buf), "#%02x%02x%02x", NS_GET_R(aColor), NS_GET_G(aColor), NS_GET_B(aColor)); CopyASCIItoUTF16(buf, aResult); }
NS_IMETHODIMP nsRenderingContextQt::InvertRect(nscoord aX,nscoord aY, nscoord aWidth,nscoord aHeight) { if (nsnull == mTranMatrix || nsnull == mSurface || nsnull == mSurface->GetGC() || nsnull == mSurface->GetPaintDevice()) return NS_ERROR_FAILURE; nscoord x,y,w,h; x = aX; y = aY; w = aWidth; h = aHeight; mTranMatrix->TransformCoord(&x,&y,&w,&h); // After the transform, if the numbers are huge, chop them, because // they're going to be converted from 32 bit to 16 bit. // It's all way off the screen anyway. ConditionRect(x,y,w,h); // Set XOR drawing mode mFunction = Qt::XorROP; UpdateGC(); // Fill the rect QColor color(NS_GET_R(mCurrentColor), NS_GET_G(mCurrentColor), NS_GET_B(mCurrentColor)); mSurface->GetGC()->fillRect(x,y,w,h,color); // Back to normal copy drawing mode mFunction = Qt::CopyROP; return NS_OK; }
void nsRenderingContextQt::UpdateGC() { QPainter *pGC; QColor color(NS_GET_R(mCurrentColor), NS_GET_G(mCurrentColor), NS_GET_B(mCurrentColor)); QPen pen(color,0,mQLineStyle); QBrush brush(color); pGC = mSurface->GetGC(); pGC->setPen(pen); pGC->setBrush(brush); pGC->setRasterOp(mFunction); if (mCurrentFont) pGC->setFont(mCurrentFont->font); if (mClipRegion) { QRegion *rgn = nsnull; pGC->setClipping(TRUE); mClipRegion->GetNativeRegion((void*&)rgn); pGC->setClipRegion(*rgn); } else { pGC->setClipping(FALSE); } }
// Function to convert RGB color space into the HSV colorspace // Hue is the primary color defined from 0 to 359 degrees // Saturation is defined from 0 to 255. The higher the number.. the deeper // the color Value is the brightness of the color. 0 is black, 255 is white. void NS_RGB2HSV(nscolor aColor, uint16_t &aHue, uint16_t &aSat, uint16_t &aValue, uint8_t &aAlpha) { uint8_t r, g, b; int16_t delta, min, max, r1, b1, g1; float hue; r = NS_GET_R(aColor); g = NS_GET_G(aColor); b = NS_GET_B(aColor); if (r>g) { max = r; min = g; } else { max = g; min = r; } if (b>max) { max = b; } if (b<min) { min = b; } // value or brightness will always be the max of all the colors(RGB) aValue = max; delta = max-min; aSat = (max!=0)?((delta*255)/max):0; r1 = r; b1 = b; g1 = g; if (aSat==0) { hue = 1000; } else { if(r==max){ hue=(float)(g1-b1)/(float)delta; } else if (g1==max) { hue= 2.0f+(float)(b1-r1)/(float)delta; } else { hue = 4.0f+(float)(r1-g1)/(float)delta; } } if(hue<999) { hue*=60; if(hue<0){ hue+=360; } } else { hue=0; } aHue = (uint16_t)hue; aAlpha = NS_GET_A(aColor); }
GdkColor nsColorPicker::convertToGdkColor(nscolor color) { GdkColor result = { 0 /* obsolete, unused 'pixel' value */, convertToGdkColorComponent(NS_GET_R(color)), convertToGdkColorComponent(NS_GET_G(color)), convertToGdkColorComponent(NS_GET_B(color)) }; return result; }
GdkRGBA nsColorPicker::convertToRgbaColor(nscolor color) { GdkRGBA result = { convertToGdkRgbaComponent(NS_GET_R(color)), convertToGdkRgbaComponent(NS_GET_G(color)), convertToGdkRgbaComponent(NS_GET_B(color)), convertToGdkRgbaComponent(NS_GET_A(color)) }; return result; }
static void SetupCairoColor(gfxContext *aContext, nscolor aRGB, float aOpacity) { aContext->SetColor(gfxRGBA(NS_GET_R(aRGB)/255.0, NS_GET_G(aRGB)/255.0, NS_GET_B(aRGB)/255.0, NS_GET_A(aRGB)/255.0 * aOpacity)); }
extern "C" NS_GFX_(void) NS_RGBToASCIIHex(nscolor aColor, nsAFlatCString& aResult) { aResult.SetLength(7); NS_ASSERTION(aResult.Length() == 7, "small SetLength failed, use an autostring instead!"); char *buf = aResult.BeginWriting(); PR_snprintf(buf, 8, "#%02x%02x%02x", NS_GET_R(aColor), NS_GET_G(aColor), NS_GET_B(aColor)); }
NS_ComposeColors(nscolor aBG, nscolor aFG) { PRIntn bgAlpha = NS_GET_A(aBG); PRIntn r, g, b, a; // First compute what we get drawing aBG onto RGBA(0,0,0,0) MOZ_BLEND(r, 0, NS_GET_R(aBG), bgAlpha); MOZ_BLEND(g, 0, NS_GET_G(aBG), bgAlpha); MOZ_BLEND(b, 0, NS_GET_B(aBG), bgAlpha); a = bgAlpha; // Now draw aFG on top of that PRIntn fgAlpha = NS_GET_A(aFG); MOZ_BLEND(r, r, NS_GET_R(aFG), fgAlpha); MOZ_BLEND(g, g, NS_GET_G(aFG), fgAlpha); MOZ_BLEND(b, b, NS_GET_B(aFG), fgAlpha); MOZ_BLEND(a, a, 255, fgAlpha); return NS_RGBA(r, g, b, a); }
NS_IMETHODIMP inFlasher::GetColor(nsAString& aColor) { // Copied from nsGenericHTMLElement::ColorToString() char buf[10]; PR_snprintf(buf, sizeof(buf), "#%02x%02x%02x", NS_GET_R(mColor), NS_GET_G(mColor), NS_GET_B(mColor)); CopyASCIItoUTF16(buf, aColor); return NS_OK; }
int32_t NS_GetLuminosity(nscolor aColor) { // When aColor is not opaque, the perceived luminosity will depend // on what color(s) aColor is ultimately drawn on top of, which we // do not know. NS_ASSERTION(NS_GET_A(aColor) == 255, "impossible to compute luminosity of a non-opaque color"); return (NS_GET_R(aColor) * RED_LUMINOSITY + NS_GET_G(aColor) * GREEN_LUMINOSITY + NS_GET_B(aColor) * BLUE_LUMINOSITY); }
//ASSURE DIFFERENT COLORS for selection inline nscolor EnsureDifferentColors(nscolor colorA, nscolor colorB) { if (colorA == colorB) { nscolor res; res = NS_RGB(NS_GET_R(colorA) ^ 0xff, NS_GET_G(colorA) ^ 0xff, NS_GET_B(colorA) ^ 0xff); return res; } return colorA; }
void nsBGColorTextAttr::Format(const nscolor& aValue, nsAString& aFormattedValue) { // Combine the string like rgb(R, G, B) from nscolor. nsAutoString value; value.AppendLiteral("rgb("); value.AppendInt(NS_GET_R(aValue)); value.AppendLiteral(", "); value.AppendInt(NS_GET_G(aValue)); value.AppendLiteral(", "); value.AppendInt(NS_GET_B(aValue)); value.Append(')'); aFormattedValue = value; }
PRBool nsBackgroundTextAttr::Get(nsAString& aValue) { // Do not expose "background-color" text attribute if its value is matched // with the default value. nscolor color = GetColor(mFrame); if (mRootFrame && color == GetColor(mRootFrame)) return PR_FALSE; // Combine the string like rgb(R, G, B) from nscolor. nsAutoString value; value.AppendLiteral("rgb("); value.AppendInt(NS_GET_R(color)); value.AppendLiteral(", "); value.AppendInt(NS_GET_G(color)); value.AppendLiteral(", "); value.AppendInt(NS_GET_B(color)); value.Append(')'); aValue = value; return PR_TRUE; }
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; }
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; }
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); }
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; }
// // 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; }
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; }
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)); }