Extracting All Color Hex Codes from Xcode Color Sets and Saving to a File
Introduction:
Most often, we the iOS Engineers get design from UI/UX team with colour code, icons etc, but what if its other way around, I recently encountered a situation where I have to work on prebuilt app and source code came from other vendor, now our design team wanted me to export all the hex colour codes used inside the application, so that the new screen designed will re-use the previously used colours etc.
Section 1: Understanding Xcode Colour Sets
Xcode colour sets is a way to add colour to you .xcasset
catalog where you generally add you icons and other images, if you want to dive deeper into it below are the few article that would help.
- https://medium.com/@sandshell811/color-assets-xcassets-in-xcode-cc24b2834f89
- https://medium.com/developerinsider/xcode-pro-tips-how-to-use-asset-catalogs-to-support-named-colors-8959ab51fd84
Lets do a hand on and create a create and Xcode project to understand that and see the structure of it in Finder(File explorer) to see how that is stored.
- Create a new Xcode project
- add few
.colorset
in assets folder, - Change the input method, three type of input method are there
a. Hex code(#00000)
b. Floating point(0.0–1.0)
c. Integer(0–255) - Finally see the asset folder in finder
Also I have video that would help to create the colorset
and do the analysis of its storage.
Section 2: Challenges Faced
Now that we already have a Xcode project if you have follow the youtube tutorial, now lets try to get the all the named color
from the project.
The noob way
- Click on the Assets
- Click on the
.colorset
(Named colour) - Click over the color square inside the Named color(could be more than one based on the appearance selected).
- Go to the attribute inspector from the Right panel(inspectors panel), and finally click on the attribute inspector.
- In attribute inspector you will find the inside the color section the color code used.
Now repeat the process for all named color, in some project it can be as high as 50 or 100 or anything.
Please also look into this stack-overflow question, which was trying to do the achieve the similar thing.
Section 3: Writing the Swift Script
Now if you have followed along, and watched the video diligently, you would know how the named colours are stored and its json format.
Let dive into writing the script or command line interface in swift.
The script should be able to do the following things
- Able to get the .asset folder path as parameter.
- Loop through all the folders in asset recursively and find all the .colorset folders name
- Read the
content.json
file inside the folder to fetch the colour codes. - Finally printing all colour codes with respect to their respective file name and appearance type.
Section 4: Running the Script
If you want to download the source code please find the link here: -https://buymeacoffee.com/ajaysinghthakur/e/277352
Or you can directly download the executable file and run.
https://github.com/ajaysinghthakur/extractnamedcolourexe
$ git clone <https://github.com/ajaysinghthakur/extractnamedcolourexe.git>
$ cd extractnamedcolourexe
$ ./ExtractNamedColours --input path_to_asset/Assets.xcassets --output output_folder_for_json
By using the above command in order, you will be able to see the resultant json.
Section 5: Exploring Output File
On running the commands from above section you would have got your obj.json
file
whose content will be similar to below
[
{
"values": [
{
"appearance": "Any",
"colorHex": "#FF0000"
},
{
"colorHex": "#FF0000",
"appearance": "luminosity",
"appearanceValue": "dark"
}
],
"color_name": "Red"
},
{
"values": [],
"color_name": "systemcolor"
},
{
"color_name": "AccentColor",
"values": []
},
{
"values": [
{
"colorHex": "#FEFFFF",
"appearance": "Any"
},
{
"appearanceValue": "dark",
"colorHex": "#000000",
"appearance": "luminosity"
}
],
"color_name": "WhiteBlack"
}
]
Json Structure
- Array of Named Colours
- Each Element in Array Contains
color_name
andvalues
- color_name: Represent the name of colour given in XCAsset.
- values: Array of element of type appearance element used, can be from one to many.
- Finally the appearance element contains
colorHex
,appearance
and option value ofappearance
.
Conclusion:
We explored Xcode color sets and how to extract named colors from an Xcode project. We discussed the challenges faced when manually retrieving color codes and then dove into writing a Swift script to automate the process. By running the provided script, you can obtain a JSON file containing all the color codes associated with their respective file names and appearance types
Support:
If you found this article insightful and want to stay updated on more iOS development tips and tricks, consider following me on:
Your support and connection are greatly appreciated, and I look forward to sharing more valuable insights with you!.