├── .gitignore ├── Copy Swift Code.sketchplugin └── Contents │ └── Sketch │ ├── manifest.json │ └── script.cocoascript ├── LICENSE ├── README.md └── screen.gif /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .*.swp 3 | -------------------------------------------------------------------------------- /Copy Swift Code.sketchplugin/Contents/Sketch/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Atsushi Nagase", 3 | "commands" : [ 4 | { 5 | "script" : "script.cocoascript", 6 | "handler" : "copyUIColor", 7 | "shortcut" : "", 8 | "name" : "Copy as UIColor", 9 | "identifier" : "copy-ui-color" 10 | }, 11 | { 12 | "script" : "script.cocoascript", 13 | "handler" : "copyNSColor", 14 | "shortcut" : "", 15 | "name" : "Copy as NSColor", 16 | "identifier" : "copy-ns-color" 17 | } 18 | ], 19 | "menu" : { 20 | "items" : [ 21 | "copy-ui-color", 22 | "copy-ns-color" 23 | ], 24 | "title" : "Copy Swift Code" 25 | }, 26 | "identifier" : "io.ngs.sketchplugin.swift-color", 27 | "version" : "1.0", 28 | "description": "Copy fill color as Swift code", 29 | "homepage": "http://github.com/ngs/sketchplugin-swift-color", 30 | "authorEmail" : "a@ngs.io", 31 | "name" : "Copy Swift Code" 32 | } 33 | -------------------------------------------------------------------------------- /Copy Swift Code.sketchplugin/Contents/Sketch/script.cocoascript: -------------------------------------------------------------------------------- 1 | var copyUIColor = function(context) { 2 | copyText(generateCode('UIColor', context)); 3 | } 4 | 5 | var copyNSColor = function(context) { 6 | copyText(generateCode('NSColor', context)); 7 | } 8 | 9 | function generateCode(initializer, context) { 10 | var selection = context.selection 11 | , res = [] 12 | , varNames = {} 13 | ; 14 | for (var i = 0; i < selection.count(); i++) { 15 | var layer = selection[i] 16 | , fills = layer.style().fills() 17 | , varName = layer.name().replace(/[^a-z0-9]/ig, '') 18 | ; 19 | varNames[varName] = varNames[varName] || 0; 20 | res.push('/* ' + layer.name() + ' */'); 21 | for (var j = 0; j < fills.count(); j++) { 22 | var fill = fills[j]; 23 | if(fill.fillType() === 0) { 24 | varNames[varName]++; 25 | var varCount = varNames[varName] 26 | , color = fill.color() 27 | ; 28 | res.push( 29 | 'let ' + varName + 30 | (varCount > 1 ? varCount : '') + 31 | ' = ' + initializer + '(' + 32 | 'red: ' + Math.round((color.red())*255) + '/255' + ', ' + 33 | 'green: ' + Math.round((color.green())*255) + '/255' + ', ' + 34 | 'blue: ' + Math.round((color.blue())*255) + '/255' + ', ' + 35 | 'alpha: ' + color.alpha() + ')' 36 | ); 37 | } 38 | } 39 | } 40 | return res.join('\n'); 41 | } 42 | 43 | function copyText(text) { 44 | var pasteBoard = [NSPasteboard generalPasteboard]; 45 | [pasteBoard declareTypes:[NSArray arrayWithObject:NSPasteboardTypeString] owner:nil]; 46 | [pasteBoard setString:text forType:NSPasteboardTypeString]; 47 | } 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Atsushi NAGASE 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Copy Swift Code [Sketch] Plugin 2 | =============================== 3 | 4 | Copy fill color as Swift code 5 | 6 | ![Screen](screen.gif) 7 | 8 | Installing 9 | ---------- 10 | 11 | Download and unarchive latest version from [Releases] page. 12 | 13 | Author 14 | ------ 15 | 16 | [Atsushi NAGASE](http://ngs.io) 17 | 18 | License 19 | ------- 20 | 21 | See [LICENSE] 22 | 23 | [Sketch]: http://www.sketchapp.com 24 | [Releases]: https://github.com/ngs/sketchplugin-swift-color/releases 25 | [LICENSE]: ./LICENSE 26 | -------------------------------------------------------------------------------- /screen.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngs-archives/sketchplugin-swift-color/57873d95e9727f2a9de5f820668f01f75f901a1d/screen.gif --------------------------------------------------------------------------------