0

I have a svg file in string format which look like this http://jsfiddle.net/mumg81qq/5/ This file contains color code in either hex/rgb format.

i want to extract these two different types of colors format in two arrays.

hex colors could be in format of

fill:#303744 
---OR---
fill="#020202"

and rgb colors could be in format of

fill:rgb(48,49,55)
---OR---
fill="rgb(205,149,36)"

resultant array should look like this

hexColor = ["#303744","#020202"]

rgbColor = ["rgb(48,49,55)","rgb(205,149,36)"]

I could only managed to write code which search one type of hex string .

string searchHex1 = "fill=\"#", searchHex2 = "fill:#";
string searchRGB1 = "fill=\"rgb(", searchRGB2 = "fill:rgb(";

List<string> hexColor = new List<string>();
List<string> rgbColor = new List<string>();

string sHexColor = "";
int index = 0;
do
{
    index = svgFile.IndexOf(searchHex1, index);
    if (index != -1)
    {
        sHexColor = svgFile.Substring(index, 7);
        if (!hexColor.Contains(sHexColor))
        {
            hexColor.Add(sHexColor);
        }
        index++;
    }
} while (index != -1);

in most efficient way i want to search 4 different types of hex & rgb colors and store it in two different arrays.

2
  • you want to use regex for this. You can get a matchcollection and then iterate it to add it to your array. Commented Apr 23, 2015 at 16:21
  • @AntoineLev could you provide code sample for the same ? Commented Apr 23, 2015 at 16:24

3 Answers 3

1

something like this should work for you where input is your file as a string

        string hexapattern = @"#[0-9a-fA-F]{6}";
        string rgbpattern = @"rgb\([0-9]+\,[0-9]+\,[0-9]+\)";

        Regex rgxHexa = new Regex(hexapattern);
        MatchCollection matches = rgxHexa.Matches(input);
        foreach (Match match in matches)
        { 
            // add to hexa array
        }


        Regex rgxRGB = new Regex(hexapattern);
        matches = rgxRGB.Matches(input);
        foreach (Match match in matches)
        {
            // add to rgb array
        }

and don't forget using System.Text.RegularExpressions;

Sign up to request clarification or add additional context in comments.

2 Comments

Will the above rgbpattern work if if i have spaces in rgb(153 , 54 , 98 ) ?
this one will match both with and without withespaces "rgb([0-9]+\,\s?[0-9]+\,\s?[[0-9]+)"
1

Would be something like this...

string hexRegex = @"fill[:=]""?(#[a-fA-F0-9]{6})""?";
string rgbRegex = @"fill[:=]""?(rgb\( *\d{1,3} *, *?\d{1,3} *, *\d{1,3} *\))""?";
string oneRegex = string.Format("({0}|{1})", hexRegex, rgbRegex);

string testdata = @"fill:#303744" + 
                  @"fill=""#020202""" +
                  @"fill:rgb(48,49,55)" +
                  @"fill=""rgb(205,149,36)""";

IEnumerable<string> colorCodes = Regex.Matches(testdata, oneRegex)
                                      .Cast<Match>()
                                      .Select(match => match.Groups[1].Value.Replace(" ",""));

Result

Comments

0

Here's the one-liner version:

var hexMatches = Regex.Matches(svg, @"(?<=fill:|="")#[0-9a-fA-F]{6}")
    .Cast<Match>().Select (m => m.Value).ToList();
var rgbMatches = Regex.Matches(svg, @"(?<=fill:|="")rgb\((\d{1,3},?){3}\)")
    .Cast<Match>().Select (m => m.Value).ToList();

You'll notice the use of positive lookbehind's:

(?<=fill:|="")

They are entirely optional and are only there so that you don't get unwanted matches in case some other color values get introduced that aren't a fill. I don't know much about the origins of the file, so I'll leave it up to you to keep them or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.