Bug 848745 part 2. Add a scriptable API for converting RGB triples to CSS color names. r=dbaron,miker
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 14 Mar 2013 15:43:00 -0400
changeset 124829 04e088286219f9723d614fa456407de4e8437123
parent 124828 daa5e00d64282d20dd9ef1a8c236ff0796c073d1
child 124830 d4a5a1e1e8e9187ec190091702039d35c433dbce
push id24436
push userryanvm@gmail.com
push dateFri, 15 Mar 2013 11:52:55 +0000
treeherdermozilla-central@8f5b1f9f5804 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdbaron, miker
bugs848745
milestone22.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 848745 part 2. Add a scriptable API for converting RGB triples to CSS color names. r=dbaron,miker
gfx/src/nsColor.cpp
gfx/src/nsColor.h
layout/inspector/public/inIDOMUtils.idl
layout/inspector/src/inDOMUtils.cpp
--- a/gfx/src/nsColor.cpp
+++ b/gfx/src/nsColor.cpp
@@ -283,8 +283,20 @@ NS_HSL2RGB(float h, float s, float l)
     m2 = l + s - l*s;
   }
   m1 = l*2 - m2;
   r = uint8_t(255 * HSL_HueToRGB(m1, m2, h + 1.0f/3.0f));
   g = uint8_t(255 * HSL_HueToRGB(m1, m2, h));
   b = uint8_t(255 * HSL_HueToRGB(m1, m2, h - 1.0f/3.0f));
   return NS_RGB(r, g, b);  
 }
+
+NS_GFX_(const char*)
+NS_RGBToColorName(nscolor aColor)
+{
+  for (size_t idx = 0; idx < ArrayLength(kColors); ++idx) {
+    if (kColors[idx] == aColor) {
+      return kColorNames[idx];
+    }
+  }
+
+  return nullptr;
+}
--- a/gfx/src/nsColor.h
+++ b/gfx/src/nsColor.h
@@ -65,9 +65,16 @@ NS_GFX_(bool) NS_LooseHexToRGB(const nsS
 // Translate a color name to a color. Return true if it parses ok,
 // otherwise return false.
 NS_GFX_(bool) NS_ColorNameToRGB(const nsAString& aBuf, nscolor* aResult);
 
 // function to convert from HSL color space to RGB color space
 // the float parameters are all expected to be in the range 0-1
 NS_GFX_(nscolor) NS_HSL2RGB(float h, float s, float l);
 
+// Return a color name for the given nscolor.  If there is no color
+// name for it, returns null.  If there are multiple possible color
+// names for the given color, the first one in nsColorNameList.h
+// (which is generally the first one in alphabetical order) will be
+// returned.
+NS_GFX_(const char*) NS_RGBToColorName(nscolor aColor);
+
 #endif /* nsColor_h___ */
--- a/layout/inspector/public/inIDOMUtils.idl
+++ b/layout/inspector/public/inIDOMUtils.idl
@@ -52,16 +52,17 @@ interface inIDOMUtils : nsISupports
   const unsigned long INCLUDE_ALIASES = (1<<1);
   void getCSSPropertyNames([optional] in unsigned long aFlags,
 			   [optional] out unsigned long aCount,
 			   [retval, array, size_is(aCount)] out wstring aProps);
 
   // Utilities for working with CSS colors
   [implicit_jscontext]
   jsval colorNameToRGB(in DOMString aColorName);
+  AString rgbToColorName(in octet aR, in octet aG, in octet aB);
 
   // DOM Node utilities
   boolean isIgnorableWhitespace(in nsIDOMCharacterData aDataNode);
   // Returns the "parent" of a node.  The parent of a document node is the
   // frame/iframe containing that document.  aShowingAnonymousContent says
   // whether we are showing anonymous content.
   nsIDOMNode getParentForNode(in nsIDOMNode aNode,
                               in boolean aShowingAnonymousContent);
--- a/layout/inspector/src/inDOMUtils.cpp
+++ b/layout/inspector/src/inDOMUtils.cpp
@@ -432,16 +432,30 @@ inDOMUtils::ColorNameToRGB(const nsAStri
   if (!triple.ToObject(aCx, nullptr, aValue)) {
     return NS_ERROR_FAILURE;
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
+inDOMUtils::RgbToColorName(uint8_t aR, uint8_t aG, uint8_t aB,
+                           nsAString& aColorName)
+{
+  const char* color = NS_RGBToColorName(NS_RGB(aR, aG, aB));
+  if (!color) {
+    aColorName.Truncate();
+    return NS_ERROR_INVALID_ARG;
+  }
+
+  aColorName.AssignASCII(color);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
 inDOMUtils::GetBindingURLs(nsIDOMElement *aElement, nsIArray **_retval)
 {
   NS_ENSURE_ARG_POINTER(aElement);
 
   *_retval = nullptr;
 
   nsCOMPtr<nsIMutableArray> urls = do_CreateInstance(NS_ARRAY_CONTRACTID);
   if (!urls)