Esempio n. 1
0
void
nsXPLookAndFeel::ColorPrefChanged (unsigned int index, const char *prefName)
{
  nsCOMPtr<nsIPrefBranch> prefService(do_GetService(NS_PREFSERVICE_CONTRACTID));
  if (prefService) {
    nsXPIDLCString colorStr;
    nsresult rv = prefService->GetCharPref(prefName, getter_Copies(colorStr));
    if (NS_SUCCEEDED(rv) && !colorStr.IsEmpty()) {
      nscolor thecolor;
      if (colorStr[0] == '#') {
        if (NS_SUCCEEDED(NS_HexToRGB(NS_ConvertASCIItoUTF16(Substring(colorStr, 1, colorStr.Length() - 1)),
                                     &thecolor))) {
          PRInt32 id = NS_PTR_TO_INT32(index);
          CACHE_COLOR(id, thecolor);
        }
      }
      else if (NS_SUCCEEDED(NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(colorStr),
                                         &thecolor))) {
        PRInt32 id = NS_PTR_TO_INT32(index);
        CACHE_COLOR(id, thecolor);
#ifdef DEBUG_akkana
        printf("====== Changed color pref %s to 0x%lx\n",
               prefName, thecolor);
#endif
      }
    }
  }
}
PRBool
nsAttrValue::ParseColor(const nsAString& aString, nsIDocument* aDocument)
{
  nsAutoString colorStr(aString);
  colorStr.CompressWhitespace(PR_TRUE, PR_TRUE);
  if (colorStr.IsEmpty()) {
    Reset();
    return PR_FALSE;
  }

  nscolor color;
  // No color names begin with a '#', but numerical colors do so
  // it is a very common first char
  if ((colorStr.CharAt(0) != '#') && NS_ColorNameToRGB(colorStr, &color)) {
    SetTo(colorStr);
    return PR_TRUE;
  }

  // Check if we are in compatibility mode
  if (aDocument->GetCompatibilityMode() == eCompatibility_NavQuirks) {
    NS_LooseHexToRGB(colorStr, &color);
  }
  else {
    if (colorStr.First() != '#') {
      Reset();
      return PR_FALSE;
    }
    colorStr.Cut(0, 1);
    if (!NS_HexToRGB(colorStr, &color)) {
      Reset();
      return PR_FALSE;
    }
  }

  PRInt32 colAsInt = static_cast<PRInt32>(color);
  PRInt32 tmp = colAsInt * NS_ATTRVALUE_INTEGERTYPE_MULTIPLIER;
  if (tmp / NS_ATTRVALUE_INTEGERTYPE_MULTIPLIER == colAsInt) {
    ResetIfSet();
    SetIntValueAndType(colAsInt, eColor);
  }
  else if (EnsureEmptyMiscContainer()) {
    MiscContainer* cont = GetMiscContainer();
    cont->mColor = color;
    cont->mType = eColor;
  }

  return PR_TRUE;
}
Esempio n. 3
0
/* void open (in nsIColorPickerShownCallback aColorPickerShownCallback); */
NS_IMETHODIMP nsColorPicker::Open(nsIColorPickerShownCallback *aColorPickerShownCallback)
{

  // Input color string should be 7 length (i.e. a string representing a valid
  // simple color)
  if (mInitialColor.Length() != 7) {
    return NS_ERROR_FAILURE;
  }

  const nsAString& withoutHash  = StringTail(mInitialColor, 6);
  nscolor color;
  if (!NS_HexToRGB(withoutHash, &color)) {
    return NS_ERROR_FAILURE;
  }

  GdkColor color_gdk = convertToGdkColor(color);

  if (mCallback) {
    // It means Open has already been called: this is not allowed
    NS_WARNING("mCallback is already set. Open called twice?");
    return NS_ERROR_FAILURE;
  }
  mCallback = aColorPickerShownCallback;

  nsXPIDLCString title;
  title.Adopt(ToNewUTF8String(mTitle));
  GtkWidget *color_chooser = gtk_color_selection_dialog_new(title);

  GtkWindow *parent_window = GTK_WINDOW(mParentWidget->GetNativeData(NS_NATIVE_SHELLWIDGET));
  if (parent_window) {
    GtkWindow *window = GTK_WINDOW(color_chooser);
    gtk_window_set_transient_for(window, parent_window);
    gtk_window_set_destroy_with_parent(window, TRUE);
  }

  gtk_color_selection_set_current_color(WidgetGetColorSelection(color_chooser),
                                        &color_gdk);

  NS_ADDREF_THIS();
  g_signal_connect(WidgetGetColorSelection(color_chooser), "color-changed",
                   G_CALLBACK(OnColorChanged), this);
  g_signal_connect(color_chooser, "response", G_CALLBACK(OnResponse), this);
  g_signal_connect(color_chooser, "destroy", G_CALLBACK(OnDestroy), this);
  gtk_widget_show(color_chooser);

  return NS_OK;
}
Esempio n. 4
0
PRBool
nsAttrValue::ParseColor(const nsAString& aString)
{
  ResetIfSet();

  // FIXME (partially, at least): HTML5's algorithm says we shouldn't do
  // the whitespace compression, trimming, or the test for emptiness.
  // (I'm a little skeptical that we shouldn't do the whitespace
  // trimming; WebKit also does it.)
  nsAutoString colorStr(aString);
  colorStr.CompressWhitespace(PR_TRUE, PR_TRUE);
  if (colorStr.IsEmpty()) {
    return PR_FALSE;
  }

  nscolor color;
  // No color names begin with a '#'; in standards mode, all acceptable
  // numeric colors do.
  if (colorStr.First() == '#') {
    nsDependentString withoutHash(colorStr.get() + 1, colorStr.Length() - 1);
    if (NS_HexToRGB(withoutHash, &color)) {
      SetColorValue(color, aString);
      return PR_TRUE;
    }
  } else {
    if (NS_ColorNameToRGB(colorStr, &color)) {
      SetColorValue(color, aString);
      return PR_TRUE;
    }
  }

  // FIXME (maybe): HTML5 says we should handle system colors.  This
  // means we probably need another storage type, since we'd need to
  // handle dynamic changes.  However, I think this is a bad idea:
  // http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2010-May/026449.html

  // Use NS_LooseHexToRGB as a fallback if nothing above worked.
  if (NS_LooseHexToRGB(colorStr, &color)) {
    SetColorValue(color, aString);
    return PR_TRUE;
  }

  return PR_FALSE;
}
Esempio n. 5
0
NS_IMETHODIMP
inFlasher::SetColor(const nsAString& aColor)
{
    NS_ENSURE_FALSE(aColor.IsEmpty(), NS_ERROR_ILLEGAL_VALUE);

    nsAutoString colorStr;
    colorStr.Assign(aColor);

    if (colorStr.CharAt(0) != '#') {
        if (NS_ColorNameToRGB(colorStr, &mColor)) {
            return NS_OK;
        }
    }
    else {
        colorStr.Cut(0, 1);
        if (NS_HexToRGB(colorStr, &mColor)) {
            return NS_OK;
        }
    }

    return NS_ERROR_ILLEGAL_VALUE;
}
Esempio n. 6
0
void
nsXPLookAndFeel::InitColorFromPref(PRInt32 i, nsIPrefBranch* aPrefBranch)
{
  nsXPIDLCString colorStr;
  nsresult rv = aPrefBranch->GetCharPref(sColorPrefs[i], getter_Copies(colorStr));
  if (NS_SUCCEEDED(rv) && !colorStr.IsEmpty())
  {
    nsAutoString colorNSStr; colorNSStr.AssignWithConversion(colorStr);
    nscolor thecolor;
    if (colorNSStr[0] == '#') {
      nsAutoString hexString;
      colorNSStr.Right(hexString, colorNSStr.Length() - 1);
      if (NS_SUCCEEDED(NS_HexToRGB(hexString, &thecolor))) {
        CACHE_COLOR(i, thecolor);
      }
    }
    else if (NS_SUCCEEDED(NS_ColorNameToRGB(colorNSStr, &thecolor)))
    {
      CACHE_COLOR(i, thecolor);
    }
  }
}
/* void init (in nsIDOMWindow parent, in AString title, in short mode); */
NS_IMETHODIMP nsColorPicker::Init(nsIDOMWindow *parent,
                                  const nsAString& title,
                                  const nsAString& initialColor)
{
  // Input color string should be 7 length (i.e. a string representing a valid
  // simple color)
  if (initialColor.Length() != 7) {
    return NS_ERROR_INVALID_ARG;
  }

  const nsAString& withoutHash  = StringTail(initialColor, 6);
  nscolor color;
  if (!NS_HexToRGB(withoutHash, &color)) {
    return NS_ERROR_INVALID_ARG;
  }

  mDefaultColor = convertToGdkColor(color);

  mParentWidget = mozilla::widget::WidgetUtils::DOMWindowToWidget(parent);
  mTitle.Assign(title);

  return NS_OK;
}
Esempio n. 8
0
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;
}