coding Shift_JIS; import GUI; import Math; import Text; import Color; import system.Int; /* -------------------------------------------------- * ここでは、起動処理や外部連携処理を記述しています。 * * -------------------------------------------------- */ /** * この関数は、プログラム開始時に自動的に実行されます。 */ public void main(){ init(); } /** * プログラムを立ち上げます。 */ public void init(){ // GUIの構築 createComponent(); // コンソールを非表示にする hide(); // 初期値のセット string code = "000000"; setColorByCode(code); } /* -------------------------------------------------- * ここでは、中核処理を記述しています。 * * -------------------------------------------------- */ /** * カラーコードを格納する文字列から色をセットします。 */ public void setColorByCode(string inCode){ // CODE項目に値を設定 setCodeField(inCode); // 先頭に「#」が付いていれば削除 if(startsWith(inCode, "#")){ inCode = substring(inCode, 1, lengthOf(inCode)); } // 16進数のプレフィックスを付加 inCode = "0x" + inCode; // 16進数として正しい表記かを検査 if(isHex(inCode)){ // 入力値をintに変換 int code = inCode; // カラーコードとして正しい範囲内か検査 if(0<=code && code<=0xffffff){ // 色配列を生成 int color[] = color(code); // 色見本ディスプレイに色を設定 setDisplayColor(color); // RED/GREEN/BLUE項目に値を設定 setRgbField( (string)color[0], (string)color[1], (string)color[2] ); }else{ alert("CODE の入力値が、#000000〜#ffffffの範囲を超えています。"); } }else{ alert("CODE の入力値が、16進数ではありません。"); } } /** * RGB値を格納する文字列から色をセットします。 */ public void setColorByRgb(string inR, string inG, string inB){ // RED/GREEN/BLUE項目に値を設定 setRgbField(inR, inG, inB); // 整数として正しい表記かを検査 if( isInt(inR) && isInt(inG) && isInt(inB) ){ // 入力値をintに変換 int r = inR; int g = inG; int b = inB; // RGB値として正しい範囲内か検査 if(0<=r&&r<=255 && 0<=g&&g<=255 && 0<=b&&b<=255){ // 色配列を生成 int color[] = {r, g, b, 255}; // 色見本ディスプレイに色を設定 setDisplayColor(color); // カラーコードの取得 string code = hex( getColorCode(color) ); code = formatColorCode(code); // CODE項目に値を設定 setCodeField(code); }else{ alert("RED / GREEN / BLUE の入力値が、0〜255の範囲を超えています。"); } }else{ alert("RED / GREEN / BLUE の入力値が、整数ではありません。"); } } /** * 16進数表記 0x〜 を、6桁のカラーコード表記に整形します。 */ private string formatColorCode(string code){ // 文字列の初期長さを取得 int length = lengthOf(code); // 先頭の 0x を削除 code = substring(code, 2, length); length -= 2; // 先頭をゼロ詰めする数 int zeros = 6 - length; // カラーコードは必ず6桁表記なので先頭をゼロ詰め for(int i=0; i>」ボタンの場合( RGB >> CODE ) if(component == rgbToCodeButton){ // RED/GREEN/BLUE項目の内容から色を設定 setColorByRgb( getComponentText(redField), getComponentText(greenField), getComponentText(blueField) ); //「<<」ボタンの場合( RGB << CODE ) }else if(component == codeToRgbButton){ // CODE項目の内容から色を設定 setColorByCode( getComponentText(codeField) ); } } /* -------------------------------------------------- * ここでは、GUIの構築を記述しています。 * * -------------------------------------------------- */ // ウィンドウのタイトルです。 private const string WINDOW_TITLE = "Color Display"; // レイアウト設定値などです。 private const int WINDOW_WIDTH = 460; private const int WINDOW_HEIGHT = 300; private const int MARGIN = 10; private const int FIELD_WIDTH = 140; private const int FIELD_HEIGHT = 30; private const int BUTTON_WIDTH = 100; private const int BUTTON_HEIGHT = 45; private const int DISPLAY_WIDTH = WINDOW_WIDTH - 40; private const int DISPLAY_HEIGHT = WINDOW_HEIGHT - FIELD_HEIGHT*3 - 80; // GUIコンポーネントIDを保持する変数です。 private int window; private int displayPanel; private int redField; private int greenField; private int blueField; private int codeField; private int rgbToCodeButton; private int codeToRgbButton; /** * 画面を構成するGUIコンポーネントを再描画します。 */ private void repaint(){ paintComponent(displayPanel); paintComponent(window); } /** * 色見本ディスプレイの色を設定します。 */ private void setDisplayColor(int color[]){ int fgColor[] = {0, 0, 0, 0}; setComponentColor(displayPanel, fgColor, color); repaint(); } /** * RED / GREEN / BLUE項目の値を設定します。 */ private void setRgbField(string r, string g, string b){ setComponentText(redField, r); setComponentText(greenField, g); setComponentText(blueField, b); repaint(); } /** * CODE項目の値を設定します。 */ private void setCodeField(string code){ setComponentText(codeField, code); repaint(); } /** * 画面を構成するGUIコンポーネントを作成し、組み立てます。 */ private void createComponent(){ // ウィンドウの作成 window = newWindow(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE); // RED/GREEN/BLUE項目パネルの作成と配置 int rgbX = MARGIN; int rgbY = MARGIN; int rgbW = FIELD_WIDTH; int rgbH = FIELD_HEIGHT*3; int rgbPanel = createRGBPanel(rgbX, rgbY, rgbW, rgbH); mountComponent(rgbPanel, window); // CODE項目パネルの作成と配置 int bpX = rgbX + FIELD_WIDTH + MARGIN; int bpY = MARGIN; int bpW = BUTTON_WIDTH; int bpH = BUTTON_HEIGHT*2; int buttonPanel = createButtonPanel(bpX, bpY, bpW, bpH); mountComponent(buttonPanel, window); // ボタンパネルの作成と配置 int codeX = bpX + BUTTON_WIDTH + MARGIN; int codeY = MARGIN + FIELD_HEIGHT; int codeW = FIELD_WIDTH; int codeH = FIELD_HEIGHT; int codePanel = createCodePanel(codeX, codeY, codeW, codeH); mountComponent(codePanel, window); // 色見本ディスプレイの作成と配置 int dpX = MARGIN; int dpY = rgbY + rgbH + MARGIN; int dpW = DISPLAY_WIDTH; int dpH = DISPLAY_HEIGHT; displayPanel = newPanel(dpX, dpY, dpW, dpH, ""); mountComponent(displayPanel, window); } /** * RED/GREEN/BLUE項目パネルを作成します。 */ private int createRGBPanel(int x, int y, int width, int height){ int panel = newGridPanel(x, y, width, height, "", 3, 2); mountComponent(newTextLabel(0, 0, 0, 0, "RED ="), panel); redField = newTextField(0, 0, 0, 0, ""); mountComponent(redField, panel); mountComponent(newTextLabel(0, 0, 0, 0, "GREEN ="), panel); greenField = newTextField(0, 0, 0, 0, ""); mountComponent(greenField, panel); mountComponent(newTextLabel(0, 0, 0, 0, "BLUE ="), panel); blueField = newTextField(0, 0, 0, 0, ""); mountComponent(blueField, panel); return panel; } /** * CODE項目パネルを作成します。 */ private int createCodePanel(int x, int y, int width, int height){ int panel = newGridPanel(x, y, width, height, "", 1, 2); mountComponent(newTextLabel(0, 0, 0, 0, "CODE = #"), panel); codeField = newTextField(0, 0, 0, 0, ""); mountComponent(codeField, panel); return panel; } /** * ボタンパネルを作成します。 */ private int createButtonPanel(int x, int y, int width, int height){ int panel = newGridPanel(x, y, width, height, "", 2, 1); rgbToCodeButton = newButton(0, 0, 0, 0, ">>"); mountComponent(rgbToCodeButton, panel); codeToRgbButton = newButton(0, 0, 0, 0, "<<"); mountComponent(codeToRgbButton, panel); return panel; }