Skip to content

Instantly share code, notes, and snippets.

@moluapple
Created October 25, 2011 03:03
Show Gist options
  • Save moluapple/1311181 to your computer and use it in GitHub Desktop.
Save moluapple/1311181 to your computer and use it in GitHub Desktop.
AI 渐变巫术师 Scriptographer 版
script.coordinateSystem = "bottom-up";
var values = {
type: '径向',
opacity: !1,
center: !0,
count: 2,
random: !0,
colormode: '随机',
start: 60,
end: 120,
color1: new CMYKColor(0, 0, 0, 0.3),
color2: new CMYKColor(0, 0, 0, 0.7),
shift: 20
},
elements = {
ruler0: {
type: 'ruler',
label: '渐变选项'
},
type: {
type: 'list',
label: '类型',
options: ['线性', '径向']
},
opacity: {
type: 'checkbox',
label: '透明度'
},
center: {
type: 'checkbox',
label: '居中中点'
},
ruler1: {
type: 'ruler',
label: ' |'
},
count: {
type: 'number',
label: '色标数',
steppers: true,
range: [2, 255],
enabled: false
},
random: {
type: 'checkbox',
label: '随机',
onChange: function (value) {
elements.count.enabled = !value;
}
},
ruler2: {
type: 'ruler',
label: ' |'
},
colormode: {
type: 'list',
label: '颜色模型',
options: ['随机', '双色循环'],
onChange: function (value) {
elements.color1.enabled = elements.color2.enabled = (value == '双色循环');
elements.start.enabled = elements.end.enabled = (value == '随机');
}
},
start: {
type: 'number',
label: '色相起点',
range: [0, 360],
steppers: true,
onChange: function (value) {
elements.end.value < value && (elements.end.value = value + 45);
}
},
end: {
type: 'number',
label: '色相终点',
range: [0, 360],
steppers: true,
onChange: function (value) {
elements.start.value > value && (elements.start.value = value - 45);
}
},
color1: {
type: 'color',
label: '颜色1',
enabled: false
},
color2: {
type: 'color',
label: '颜色2',
enabled: false
},
excute: {
type: 'button',
value: '创建渐变',
onClick: createGradient
},
ruler3: {
type: 'ruler',
label: '附加功能'
},
shift: {
type: 'number',
label: '阀值',
steppers: true,
increment: 5,
range: [10, 30],
},
convert: {
type: 'button',
value: '渐变<->实色',
onClick: function () {
document.selectedItems.each(function (path) {
if (path.fillColor.type == 'cmyk') convertToGrad(path)
else path.fillColor.gradient && convertToFlat(path);
});
}
},
ruler4: {
type: 'ruler',
label: ' |'
},
relate: {
type: 'list',
label: '参照',
options: ['顶层路径', '最小路径']
},
align: {
type: 'button',
value: '渐变同一化',
onClick: function () {
var topmost = document.selectedItems[0],
smallest = document.selectedItems.sort(function (a, b) {
return a.bounds.width * a.bounds.height > b.bounds.width * b.bounds.height
})[0],
f = function (a) {
return new GradientColor(a.gradient, a.origin, a.destination, a.hilite, a.matrix)
};
document.selectedItems.each(function (item) {
item.fillColor = elements.relate.value == '顶层路径' ? f(topmost.fillColor) : f(smallest.fillColor)
});
}
},
menuEntry: {
type: 'menu-entry',
value: '关于作者',
onSelect: function () {
Dialog.alert(['-- animalia (跳入苹果)', 'http://applia.tumblr.com', '特别感谢 Scriptographer 创作者', 'http://scriptographer.org'].join('\n'));
}
}
},
palette = new Palette('AI 渐变巫术师', elements, values);
function createGradient() {
var _type = ['linear', 'radial'][elements.type.selectedIndex],
opacity = elements.opacity.value,
center = elements.center.value,
count = elements.count.value,
random = elements.random.value,
colormode = elements.colormode.value,
start = elements.start.value,
end = elements.end.value,
oddColor = elements.color1.value,
evenColor = elements.color2.value,
gradient = new Gradient() {
type: _type,
stops: genStops()
},
origin = new Point(0, _type == 'linear' ? -100 : 0),
destination = new Point(0, 100),
gradientColor = new GradientColor(gradient, origin, destination),
circle = new Path.Circle(new Point(0, 0), 100) {
fillColor: gradientColor,
strokeColor: null
}
function genStops() {
var stops = [],
rgb, color, rampPoint, midPoint, stopsN = (random ? Math.ceil((Math.random() * 255)) : count),
i;
for (i = 0; i < stopsN; i++) {
rgb = hsbToRgb([assignRandom(start, end), Math.random() * 100, 100]);
color = colormode == '随机' ? new RGBColor(rgb[0] / 255, rgb[1] / 255, rgb[2] / 255) : i % 2 ? oddColor : evenColor;
color.alpha = opacity ? Math.random() : 1;
rampPoint = i / (stopsN - 1);
midPoint = center ? 0.5 : Math.random() * 0.74 + 0.13;
stops.push(new GradientStop(color, rampPoint, midPoint));
}
return stops
}
}
function assignRandom(minr, maxr) {
rand = minr + Math.random() * (maxr - minr);
return rand;
}
function hsbToRgb(hsb) {
var br = Math.round(hsb[2] / 100 * 255);
if (hsb[1] == 0) {
return [br, br, br];
} else {
var hue = hsb[0] % 360;
var f = hue % 60;
var p = Math.round((hsb[2] * (100 - hsb[1])) / 10000 * 255);
var q = Math.round((hsb[2] * (6000 - hsb[1] * f)) / 600000 * 255);
var t = Math.round((hsb[2] * (6000 - hsb[1] * (60 - f))) / 600000 * 255);
switch (Math.floor(hue / 60)) {
case 0:
return [br, t, p];
case 1:
return [q, br, p];
case 2:
return [p, br, t];
case 3:
return [p, q, br];
case 4:
return [t, p, br];
case 5:
return [br, p, q];
}
}
}
function convertToGrad(path) {
var shift = elements.shift.value / 100,
_type = ['linear', 'radial'][elements.type.selectedIndex],
cmyk = path.fillColor,
startColor = new CMYKColor([
(cmyk.cyan - shift) > 0 ? (cmyk.cyan - shift) : 0,
(cmyk.magenta - shift) > 0 ? (cmyk.magenta - shift) : 0,
(cmyk.yellow - shift) > 0 ? (cmyk.yellow - shift) : 0,
(cmyk.black - shift) > 0 ? (cmyk.black - shift) : 0
]),
endColor = new CMYKColor([
(cmyk.cyan + shift) < 1 ? (cmyk.cyan + shift) : 1,
(cmyk.magenta + shift) < 1 ? (cmyk.magenta + shift) : 1,
(cmyk.yellow + shift) < 1 ? (cmyk.yellow + shift) : 1,
(cmyk.black + shift) < 1 ? (cmyk.black + shift) : 1
]),
gradient = new Gradient() {
type: _type,
stops: [new GradientStop(startColor, 0), new GradientStop(endColor, 1)]
},
origin = path.bounds.leftCenter,
destination = path.bounds.rightCenter,
gradientColor = new GradientColor(gradient, origin, destination);
path.fillColor = gradientColor;
}
function convertToFlat(path) {
var c = m = y = k = 0,
stops = path.fillColor.gradient.stops;
stops.each(function (stop) {
c += stop.color.cyan;
m += stop.color.magenta;
y += stop.color.yellow;
k += stop.color.black;
});
c = c / stops.length, m = m / stops.length, y = y / stops.length, k = k / stops.length, path.fillColor = new CMYKColor(c, m, y, k);
}
@moluapple
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment