xcodecolorsassets

Search color by hex code in xcode Colours asset


How can i search in Xcode if a color like #4D4D4D is already present within the assets? I have tried searching by the hex code but does not show anything.


Solution

  • This is not possible using Xcode alone, but it is possible using Terminal commands.

    Assume, you define in your assets a new color set. If you open the Inspector and select this set, you can define the color by a hex pattern:
    enter image description here

    If you then select this color in the left pane and choose „Show in Finder“, the Finder will show a folder that contains a single file named Contents.json. This file contains the RGB colors in hex:

    {
      "info" : {
        "version" : 1,
        "author" : "xcode"
      },
      "colors" : [
        {
          "idiom" : "universal",
          "color" : {
            "color-space" : "srgb",
            "components" : {
              "red" : "0x12",
              "alpha" : "1.000",
              "blue" : "0x56",
              "green" : "0x34"
            }
          }
        }
      ]
    }
    

    In order to find all colors defined as #1234356, one had to search the asset catalog for files that contain all of the following strings: "red" : "0x12", "green" : "0x34", and "blue" : "0x56". But Xcode does not allow to specify such an AND search within a file.

    This is, however, possible using terminal commands. This post shows a number of examples using variants of grep and awk commands that use some kind of regex to express the AND condition within a file. A one-liner mentioned in this answer is:

    awk 'NR==FNR{a[$0];next} {for(s in a) if(!index($0,s)) exit 1}' strings RS='^$' file
    

    The syntax might be a little cryptic, but it should work.

    In order to find all files in the asset catalog, you had to specify as the search path this catalog (easiest by drawing this folder from the Finder to the terminal command), and to specify a recursive search.

    It might be necessary to install an appropriate command, since not all of them are pre-installed. PS: I did not try it by myself, so good luck!