Skip to content

Commit

Permalink
Add theme management. Add stettings js. Update the plugin version
Browse files Browse the repository at this point in the history
  • Loading branch information
icona79 committed May 5, 2022
1 parent da767a3 commit 0834edc
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 205 deletions.
60 changes: 53 additions & 7 deletions DS-Core.sketchplugin/Contents/Sketch/color-functions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
function increase_brightness(hex, percent) {
// strip the leading # if it's there
hex = hex.replace(/^\s*#|\s*$/g, "");
// **********************************************************
// General color functions for all the scripts
// import via @import(@import "color-functions.js") at the very
// beginning of your script (after the var onRun() statement)
// **********************************************************

// convert 3 char codes --> 6, e.g. `E0F` --> `EE00FF`
if (hex.length == 3) {
hex = hex.replace(/(.)/g, "$1$1");
}
/// Brightness management for color scales
function increase_brightness(hex, percent) {
hex = standardizeHex(hex);

let r = parseInt(hex.substr(0, 2), 16);
let g = parseInt(hex.substr(2, 2), 16);
Expand Down Expand Up @@ -38,6 +39,22 @@ function increase_brightness(hex, percent) {
return RGBToHex(r, g, b);
}

/// Invert Colors
function invertColor(hex) {
hex = standardizeHex(hex);

if (hex.length !== 6 && hex.length !== 8) {
throw new Error("Invalid HEX color.");
}
// invert color components
var r = 255 - parseInt(hex.slice(0, 2), 16);
var g = 255 - parseInt(hex.slice(2, 4), 16);
var b = 255 - parseInt(hex.slice(4, 6), 16);
var a = hex.slice(6, 8);

return RGBToHex(r, g, b) + a;
}

/// RGBToHex
function RGBToHex(r, g, b) {
r = r.toString(16);
Expand Down Expand Up @@ -140,6 +157,16 @@ function HSLToRGB(h, s, l) {
return [r, g, b];
}

/// Standardize HEX
function standardizeHex(hex) {
hex = hex.replace(/^\s*#|\s*$/g, "");
// convert 3 char codes --> 6, e.g. `E0F` --> `EE00FF`
if (hex.length == 3 || hex.lenght == 4) {
hex = hex.replace(/(.)/g, "$1$1");
}
return hex;
}

// Update Color Variables
function updateLayerWithColorVariables(context) {
// When you open an existing document in Sketch 69, the color assets in the document will be migrated to Color Swatches. However, layers using those colors will not be changed to use the new swatches. This plugin takes care of this
Expand Down Expand Up @@ -194,4 +221,23 @@ function matchingSwatchForColor(color, name) {
function colorVariableFromColor(color) {
let swatch = matchingSwatchForColor(color);
return swatch.referencingColor;
}

function createColorVariable(colorName, color) {
const documentColors = sketch.getSelectedDocument().colors;
// Generate the Color Variable, if not existent
if (sketchversion >= 69) {
var arrayColorVarNames = document.swatches.map((Swatch) => Swatch["name"]);
const Swatch = sketch.Swatch;
var newSwatch = Swatch.from({ name: colorName, color: color });

if (arrayColorVarNames.indexOf(colorName) === -1) {
document.swatches.push(newSwatch);
}
} else {
var arrayColorAssetsNames = documentColors.map((ColorAsset) => ColorAsset["name"]);
if (arrayColorAssetsNames.indexOf(colorName) === -1) {
documentColors.push({ type: "ColorAsset", name: colorName, color: color });
}
}
}
4 changes: 2 additions & 2 deletions DS-Core.sketchplugin/Contents/Sketch/create-color-scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var onRun = function(context) {
return;
} else {
// sketch.UI.message(value);
sketch.UI.message("🌈: Yay! " + value.replace("Steps","Color steps created! 👏 🚀"));
sketch.UI.message("🌈: Yay! " + value.replace("Steps", "Color steps created! 👏 🚀"));


let result = colorVariations[labels.indexOf(value)];
Expand Down Expand Up @@ -122,4 +122,4 @@ var onRun = function(context) {
} else {
sketch.UI.message("☝️ Please select at least one layer.");
}
};
};
Loading

0 comments on commit 0834edc

Please sign in to comment.