5

I'm trying to format a color in hex for use in HTML, running ClojureScript in the browser.

Here's my "format" function.

(defn gen-format [& args] (apply gstring/format args) )

in a "strings" namespace where I've required the goog.string library with :

(:require [goog.string :as gstring] [goog.string.format :as gformat])

But when I try to call it from javascript :

document.write(mypackage.strings.gen_format("#%x%x%x",0,0,0));

it just returns #%x%x%x

It's not crashing. But the goog format function doesn't seem to be substituting the values in. Am I doing something wrong here?

interstar
  • 26,048
  • 36
  • 112
  • 180

1 Answers1

6

What does %x do?

Looking at format source sorce, it only supports s, f, d, i and u:

var formatRe = /%([0\-\ \+]*)(\d+)?(\.(\d+))?([%sfdiu])/g;

This seems to be working well for me:

mypackage.strings.gen_format("#%d%d%d", 0, 0, 0)

UPDATE: If you need to render a string with color, how about these:

(defn hex-color [& args]
  (apply str "#" (map #(.toString % 16) args))

(defn hex-color [r g b]
  (str "#" (.toString r 16) (.toString g 16) (.toString b 16))
Konrad Garus
  • 53,145
  • 43
  • 157
  • 230
  • %x is meant to be hexadecimal. It works with the Clojure version of string formatting. Maybe it's not supported in Javascript. :-( – interstar Jul 09 '14 at 19:18
  • In this case I guess it's something that Java supports (mind you, it's pretty rich), but Google Closure does not. See http://dev.clojure.org/jira/browse/CLJS-324. – Konrad Garus Jul 10 '14 at 04:13
  • This is not a real answer... The original problem still persists. – n2o Jan 19 '19 at 17:03