javascript - Count all hex colour codes in SVG/string -
I have a SVG image and I want to get all the color hex codes in an array. So in this case I have 3 different colors. Nevertheless, overall I have 8 because it is repetition. I can count with everyone. And obviously I get another incident of more than eight #. Whatever I can think of my problem is very heavy. If there is one then I need a simple solution, is there an easy way to achieve this? Getting all the hex codes without any duplicates will be wonderful. Edit: OK, then iterate through the array and check that it is valid using the hex code. then But how do I create an initial array, this is the part with which I am struggling. Will I use indexOf () in the initial recurrence? It's all very dirty. How about it? You must also keep in mind that "# 00F", "# 00f" and "# 0000ff" are all equal colors but differently I will assume that here I have assumed that these files are created by an editor or something similar in that this is how it lists the colors. & lt; Svg width = "640" height = "480" xmlns = "http://www.w3.org/2000/svg" xmlns: xlink = "http: // www .w3.org / 1999 / XLink" & gt; & Lt; G display = "inline" & gt; & Lt; Title & gt; Layer 1 & lt; / Title & gt; & Lt; Ellipse ry = "116" rx = "66" id = "svg_1" cy = "130.7" cx = "375.2" stroke-width = "3" stroke = "# 000000" Fill = "# FF 20000" /> & Lt; Ellipse ry = "104" rx = "68" id = "svg_2" cy = "133.7" cx = "248.2" stroke-width = "3" stroke = "# 000000" Fill = "# FF 20000" /> & Lt; Ellipse ry = "73" rx = "47" id = "svg_3" cy = "161.7" cx = "231.2" stroke-width = "3" stroke = "# 000000" Fill = "# FF 20000" /> & Lt; Rect id = "svg_4" height = "77" width = "83" y = "66.7" x = "225.2" stroke-width = "3" stroke = "# 000000" fill = "# 0000ff" /> & Lt; / G> & Lt; / Svg & gt;
function count_colours (data) {return data.match (/ # / g) .length; }
var isOk = / (^ # [ 0- 9A-F] {6} $). Remove the duplicate using (^ # [[0- 9A-F] {3} $) / i.test ('# XXXXX')
_unic (hex_codes)
function count_colours (data) {var N = 0, mail, cols = {}; If (matches = data match (/ \ # [0- 9AAFA-F] {3} ([0- 9A-FA-F] {3})? / G)) {for (var i = 0; I & lt; matches.length; i ++) {if (! Cols [matches [i]]) {column [matches [i]] = 1 n ++; }}} Return n; }
Comments
Post a Comment