////////////////////////////////////////////////////////////////////////////
// ADOBE SYSTEMS INCORPORATED
// Copyright 2007 Adobe Systems Incorporated
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the
// terms of the Adobe license agreement accompanying it. If you have received this file from a
// source other than Adobe, then your use, modification, or distribution of it requires the prior
// written permission of Adobe.
/////////////////////////////////////////////////////////////////////////////

/**
@fileoverview Shows how to use graphic objects to customize the drawing of ScriptUI elements.
@class Shows how to use graphic objects to customize the drawing of ScriptUI elements.

Usage

  1. Open this file and run it in the ExtendScript Toolkit.
    You can choose as the target any application that supports ScriptUI

  2. Move the sliders up and down to change the color of the panels
  3. Click the large swatch to pop an alert with the current RGB settings

Description

This script creates an example Color Picker floating palette to show how to use some
of the ScriptUI graphics properties and methods. Specifically, it demonstrates:

  • Defining a ScriptUIFont object and using it to set an element’s graphics.font property.
  • Defining a ScriptUIBrush object and using it to set an element’s graphics.backgroundColor property.
  • Dttaching an onDraw notify handler to a UI element and drawing the element in different ways.
  • Defining linear segment and rectangular paths.
  • Filling a path with a ScriptUIBrush created from a dynamic RGB color.
  • Stroking over paths with ScriptUIPen objects created from static colors (using partial transparency for some pens).

See the JavaScript Tools Guide for more details.

@see ColorSelector.jsx

@constructor Constructor
*/
function ColorPicker() {}

/**
此示例的函数部分,创建新类型的多个节点和一个管理器对象,
它存储标识前缀和根节点的节点。

@如果样本按预期运行,则返回true,否则返回false
@type Boolean
*/
ColorPicker.prototype.run = function()
{

// Constants
/*
要使用的字体大小.
@type Mumber
*/
var kFontSize = 9;

/*
主色样的线条宽度(以像素为单位).
@type Number
*/
var kSwatchBorderWidth = 2;

/*
RGB选取器组件类实现了RGB颜色选取器类的单个颜色组件。
它使用Slider和EditText元素让用户控制单个RGB组件颜色的级别。
*/
function RGBPickerComponent (componentIndex)
{
// 定义的用户界面元素和布局
this.uiResource =
“””group {
spacing: 2,
alignChildren:[‘right’,’center’],
swatch: Panel {
alignment:[‘left’,’center’],
properties:{ borderStyle:’sunken’ },
preferredSize:[18,18]
},
label: StaticText { },
slColor: Slider {
minvalue:0, maxvalue:100
},
etColor: EditText {
characters:3, justify:’right’
},
percent: StaticText { text:’%’ }
}”””;
this.initRGBPickerComponent (componentIndex);
}

RGBPickerComponent.prototype.initRGBPickerComponent =
function (componentIndex)
{
this.rgbVal = [0, 0, 0];
this.componentIndex = componentIndex;
}

RGBPickerComponent.prototype.initialize = function (pickerObj, colorLabel, initialValue)
{
this.pickerObject = pickerObj;
this.ui = pickerObj.getContainer().add (this.uiResource);
this.ui.rgbComponentObj = this; // 将UI对象链接回此对象

with (this.ui) {
label.graphics.font = pickerObj.getUIFont();
label.text = colorLabel;
slColor.value = initialValue * slColor.maxvalue;
slColor.onChanging = slColor.onChange = function ()
{
// “this”是滑块元素
this.parent.etColor.text = this.value;
this.parent.rgbComponentObj.updateSwatch();
}
etColor.graphics.font = pickerObj.getUIFont();
etColor.onChange = function ()
{
/* “this”是editText元素:将其值限制在滑块的最小值和最大值之间
*/
var slider = this.parent.slColor;
var val = Number (this.text);
if (val > slider.maxvalue)
val = slider.maxvalue;
else if (val < slider.minvalue) val = slider.minvalue; slider.value = val; this.text = val; this.parent.rgbComponentObj.updateSwatch(); } } this.setComponentValue (initialValue); } RGBPickerComponent.prototype.getComponentValue = function () { return this.rgbVal[this.componentIndex]; } RGBPickerComponent.prototype.setComponentValue = function (colorVal) { this.ui.slColor.value = colorVal * this.ui.slColor.maxvalue; this.ui.slColor.notify ("onChange"); } RGBPickerComponent.prototype.updateSwatch = function () { this.rgbVal[this.componentIndex] = this.ui.slColor.value / this.ui.slColor.maxvalue; var uiGfx = this.ui.graphics; this.ui.swatch.graphics.backgroundColor = uiGfx.newBrush (uiGfx.BrushType.SOLID_COLOR, this.rgbVal); this.ui.swatch.graphics.disabledBackgroundColor = this.ui.swatch.graphics.backgroundColor; this.pickerObject.updateRGBSwatch(); } /* GrayPicker类是从rgbPickerComponent类派生的。 它允许用户在用户更改单个滑块或编辑文本字段时 同时更改红色、绿色和蓝色组件值,从而选择灰色阴影。 它有一个不可见的样本,因为我们不想预览灰色阴影 (主选取器的样本会显示这个) */ function GrayPicker () { this.initGrayPicker(); } // 从rgbpickercomponent派生,“override”其初始化方法。 GrayPicker.prototype = new RGBPickerComponent (0); GrayPicker.prototype.initializeRGBPickerComponent = GrayPicker.prototype.initialize; GrayPicker.prototype.initGrayPicker = function () { this.initRGBPickerComponent (0); } GrayPicker.prototype.initialize = function (pickerObj, colorLabel, initialGrayLevel) { this.initializeRGBPickerComponent (pickerObj, colorLabel, initialGrayLevel); this.ui.swatch.visible = false; } /* 将所有样本更新为选定的灰色阴影 */ GrayPicker.prototype.updateSwatch = function () { var grayLevel = this.ui.slColor.value / this.ui.slColor.maxvalue; this.pickerObject.enableUpdates (false); this.pickerObject.redPicker.setComponentValue (grayLevel); this.pickerObject.greenPicker.setComponentValue (grayLevel); this.pickerObject.bluePicker.setComponentValue (grayLevel); this.pickerObject.enableUpdates (true); this.pickerObject.updateRGBSwatch(); } /* rgbColorPicker类使用了一个GrayPicker实例和rgbPickerComponent 创建RGB颜色选取器用户界面的实例。 */ function RGBColorPicker (container, initialRGB, fontSize) { this.initRGBColorPicker (container, initialRGB, fontSize); } /* 初始化默认值,设置组容器的方向并添加RGB选取器组件 */ RGBColorPicker.prototype.initRGBColorPicker = function (container, initialRGB, fontSize) { this.container = container; this.rgbValue = initialRGB; this.uiFont = ScriptUI.newFont ("palette", fontSize); this.updatesEnabled = false; // 为每个RGB组件颜色创建选择器UI container.orientation = 'column'; container.alignChildren = ['fill','top']; container.spacing = 2; this.redPicker = new RGBPickerComponent (0); this.greenPicker = new RGBPickerComponent (1); this.bluePicker = new RGBPickerComponent (2); this.enableUpdates (false); this.redPicker.initialize (this, "R:", initialRGB[0]); this.greenPicker.initialize(this, "G:", initialRGB[1]); this.bluePicker.initialize(this,"B:", 2, initialRGB[2]); this.enableUpdates (true); this.grayPicker = new GrayPicker(); this.grayPicker.initialize (this, "Gray:", 1); this.updateRGBSwatch(); } RGBColorPicker.prototype.getContainer = function () { return this.container; } RGBColorPicker.prototype.getUIFont = function () { return this.uiFont; } RGBColorPicker.prototype.getRGBColor = function () { return this.rgbValue; } RGBColorPicker.prototype.enableUpdates = function (enable) { this.updatesEnabled = enable; } RGBColorPicker.prototype.updateRGBSwatch = function () { if (this.updatesEnabled) { this.rgbValue[0] = this.redPicker.getComponentValue(); this.rgbValue[1] = this.greenPicker.getComponentValue(); this.rgbValue[2] = this.bluePicker.getComponentValue(); var swatchGfx = this.container.window.rgbSwatch.graphics; swatchGfx.backgroundColor = swatchGfx.newBrush (swatchGfx.BrushType.SOLID_COLOR, this.rgbValue); swatchGfx.disabledBackgroundColor = swatchGfx.backgroundColor; this.container.window.rgbSwatch.btn.bgPen = swatchGfx.newPen (swatchGfx.PenType.SOLID_COLOR, this.rgbValue, kSwatchBorderWidth); } } // 创建颜色选取器窗口 var colorPickerRes = "palette { \ text: '颜色选取器演示', \ properties: { closeOnKey:'OSCmnd+W', independent:true }, \ orientation:'row', \ rgbSwatch: Group { \ btn: IconButton { text: 'Ncj功能1',preferredSize:[40,40] } \ }, \ rgbPicker: Group { }, \ }"; var colorPickerWin = new Window (colorPickerRes); colorPickerWin.rgbPicker = new RGBColorPicker (colorPickerWin.rgbPicker, [1, 1, 1], kFontSize); colorPickerWin.rgbSwatch.btn.onDraw = drawRGBSwatch; colorPickerWin.rgbSwatch.btn.onClick = clickRGBSwatch; colorPickerWin.rgbSwatch.btn.text = '测试2222' /* 强制初始布局,使UI元素尺寸已知,并强制样本组 紧紧围绕按钮(覆盖布局管理器的默认值)*/ colorPickerWin.layout.layout (true); with (colorPickerWin.rgbSwatch) { size = btn.size; location = [btn.windowBounds.x, btn.windowBounds.y]; btn.location = [0, 0]; } initializeDrawingState (colorPickerWin.rgbSwatch.btn);//initializeDrawingState // 现在显示窗口 colorPickerWin.show(); /* RGB样本作为一个组元素实现,其中有一个按钮可以完全填充它。 我们使用组是因为可以设置组的graphics.backgroundcolor, 所以我们将其设置为当前选定的RGB颜色。 我们在这个组中放置一个按钮来演示如何使样本“活动”, 并通过为它定义一个OnDraw处理程序来显示一种使按钮基本透明的技术, 该处理程序不绘制按钮的“背景”,只绘制按钮的边框。 我们初始化“RGB样本”按钮的“静态”绘图状态,如下所示:

  • “border”路径是常量,因此现在创建它们,而不是每次绘制样本时都创建它们。
  • 同样,边框的笔是不变的颜色,因此只能创建一次。

*/
function initializeDrawingState (swatchBtn)
{

var gfx = swatchBtn.graphics;
var btnW = swatchBtn.size.width;
var btnH = swatchBtn.size.height;
// 定义左上和右下边界路径
var halfBorderW = kSwatchBorderWidth / 2;
gfx.newPath();
gfx.moveTo (halfBorderW, btnH – halfBorderW);
gfx.lineTo (halfBorderW, halfBorderW);
gfx.lineTo (btnW – halfBorderW, halfBorderW);
swatchBtn.tlBorderPath = gfx.currentPath;
gfx.newPath();
gfx.moveTo (halfBorderW, btnH – halfBorderW);
gfx.lineTo (btnW – halfBorderW, btnH – halfBorderW);
gfx.lineTo (btnW – halfBorderW, halfBorderW);
swatchBtn.brBorderPath = gfx.currentPath;
// 定义边框笔:使用半透明笔,使背景色通过
swatchBtn.highlightPen = gfx.newPen (gfx.PenType.SOLID_COLOR, [1, 1, 1, .4], kSwatchBorderWidth);
swatchBtn.shadowPen = gfx.newPen (gfx.PenType.SOLID_COLOR, [.25, .25, .25, .4], kSwatchBorderWidth);
// swatchBtn.textPen = gfx.newPen (gfx.PenType.THEME_COLOR, [.25, .25, .25, .4], kSwatchBorderWidth);
// swatchBtn.shadowPen = gfx.newPen (gfx.PenType.SOLID_COLOR, [.25, .25, .25, .4], kSwatchBorderWidth);

}

/*
这是RGB样本按钮的“OnDraw”事件处理程序函数。因为这个函数是
每次绘制此按钮时调用,我们希望它尽可能快地执行,以便
尽可能多的绘图状态是在这个函数的“外部”派生的。
*/
function drawRGBSwatch (drawingStateObj)
{
var gfx = this.graphics;
try {
//不要绘制按钮背景-让我们的容器的背景色显示出来。只需画出边界,
//基于鼠标状态:首先用容器中的纯色bg绘制边框下方,
//然后使用半透明的边框高光和阴影在顶部绘制.
this.text = ‘ccc’
// gfx.text = gfx.add(“StaticText”);
gfx.strokePath (this.bgPen, this.tlBorderPath);
gfx.strokePath (this.bgPen, this.brBorderPath);
if (drawingStateObj.leftButtonPressed) {
gfx.strokePath (this.shadowPen, this.tlBorderPath);
gfx.strokePath (this.highlightPen, this.brBorderPath);
}
else {
gfx.strokePath (this.highlightPen, this.tlBorderPath);
gfx.strokePath (this.shadowPen, this.brBorderPath);
}
}
catch (e) {
// 如果出现任何错误,请取消定义OnDraw处理程序,这样我们就不会再出现这种情况。
this.onDraw = undefined;
alert (“绘制RGB样本处理程序失败.\n” + e);
}
}

// 这是RGB样本按钮的“onclick”事件处理程序
function clickRGBSwatch ()
{
var rgb = this.window.rgbPicker.getRGBColor();
alert (“选定的RGB颜色:\n ” + rgb[0] * 100 + “% red ” +
rgb[1] * 100 + “% green ” +
rgb[2] * 100 + “% blue”);
}

}

/**
“主程序”:构造一个匿名实例并运行它
*/
if(typeof(ColorPicker_unitTest) == “undefined”) {
new ColorPicker().run();
}

Leave a Reply

Your email address will not be published.

请下载到本地观看!因为pdf是用的阿里云盘预览,可能部分字体加载不了。

请安装油猴脚本,并下载月离的万事屋脚本

请在右上角三个点 - 下载管理 - 找到刚才的文件,选择允许下载。 或者换个浏览器
echo