/* * === Color === * * updated 2003/07/02 * * mail : peace@skipup.com * home : http://www.skipup.com/~peace/ */ /** * 色. * * インスタンス変数. * int value 0xAArrGGbb. */ function class__JColor__(window) { var classId = Object.getClassName(arguments.callee); /** * 0〜255 */ function checkRange(v) { if (0 <= v && v <= 255) return v; else { alert("Error@checkRange"); return void 0; } } /** * String toHexString(int v) * @param v 0 <= v <= 255であることが保障されている必要がある. */ function toHexString(v) { return v < 0x10 ? "0" + v.toString(16) : v.toString(16); } function apply(o, a) { switch (a.length) { case 0 : o.value = 0xff000000; return; case 1 : { // int || JColor || Array || Arguments || String var s = a[0]; switch ((typeof s).charAt(0)) { case 'n' : // int o.value = 0xff000000 | (s & 0xffffff); return; case 'o' : // JColor || Array || Arguments if (s.constructor === F) { o.value = s.value; return; } else if (s.constructor === Array) { o.value = 0xff000000 | (checkRange(s[0]) << 16) | (checkRange(s[1]) << 8) | checkRange(s[2]); return; } else if (s.callee !== void 0) { apply(o, s); return; } return; case 's' : // String o.value = 0xff000000 | (parseInt("0x" + (s.charAt(0) == '#' ? s.substring(1) : s)) & 0xffffff); return; } return; } case 2 : if (a[1]) o.value = a[0] & 0xffffffff; else o.value = 0xff000000 | (a[0] & 0xffffff); return; case 3 : // int, int, int o.value = 0xff000000 | (checkRange(a[0]) << 16) | (checkRange(a[1]) << 8) | checkRange(a[2]); return; case 4 : // int, int, int, int (r, g, b, a) o.value = (checkRange(a[3]) << 24) | (checkRange(a[0]) << 16) | (checkRange(a[1]) << 8) | checkRange(a[2]); return; } } /** * コンストラクタ. * JColor() * JColor(int rgb) * JColor(int argb, boolean hasAlpha) * JColor(byte[] rgb) * JColor(String rgb) * @param rgb "RRggBB" 又は "#RRggBB" * JColor(JColor color) * JColor(arguments arg) * JColor(byte red, byte green, byte blue) * JColor(byte red, byte green, byte blue, int alpha) */ var F = window[classId] = function() { if (this.constructor !== F) this.constructor = F; this.value = 0xff000000; apply(this, arguments); }; var FP = F.prototype; F.BLACK = new F(0x000000); F.GRAY = new F(0x808080); F.SILVER = new F(0xc0c0c0); F.WHITE = new F(0xffffff); F.MAROON = new F(0x800000); F.RED = new F(0xff0000); F.PURPLE = new F(0x800080); F.FUCHSIA = new F(0xff00ff); F.GREEN = new F(0x008000); F.LIME = new F(0x00ff00); F.OLIVE = new F(808000); F.YELLOW = new F(0xffff00); F.NAVY = new F(0x000080); F.BLUE = new F(0x0000ff); F.TEAL = new F(0x008080); F.AQUA = new F(0x00ffff); /** * RGB形式をHSB形式に変換. * * float[] RGB2HSB(int rgb, float[] hsb) * float[] RGB2HSB(JColor rgb, float[] hsb) * @return hsbの参照.[h, s, b]. * * float[] RGB2HSB(int rgb) * float[] RGB2HSB(JColor rgb) * @return [h, s, b]. */ F.RGB2HSB = function(rgb, hsb) { if (hsb === void 0) hsb = new Array(3); var _r, _g, _b; if (typeof a[0] == "number") { _r = (rgb >>> 16) & 0xff; _g = (rgb >>> 8) & 0xff; _b = rgb & 0xff; } else { _r = rgb.getRed(); _g = rgb.getGreen(); _b = rgb.getBlue(); } var cmax = Math.max(_r, _g, _b), cmin = Math.min(_r, _g, _b); var h = 0.0, s = cmax != 0.0 ? (cmax - cmin) / cmax : 0.0, b = cmax / 0xff; if (s != 0.0) { var x = (cmax - _r) / (cmax - cmin), y = (cmax - _g) / (cmax - cmin), z = (cmax - _b) / (cmax - cmin); if (r == cmax) h = 0.0 + z - y; else if (g == cmax) h = 2.0 + x - z; else h = 4.0 + y - x; h /= 6.0; if (h < 0.0) h += 1.0; else if (h > 1.0) h -= 1.0; } hsb[0] = h; hsb[1] = s; hsb[2] = b; return hsb; }; /** * HSB形式をRGB形式に変換. * int HSB2RGB(float h, float s, float b) * @return 0xffRRggBB.透明度要素を含むことに注意. */ F.HSB2RGB = function(h, s, b) { // Hue:色相, Saturation:彩度, Brightness:明度 var _r, _g, _b; if (s == 0.0) { _r = _g = _b = b; } else { var e = (h - Math.floor(h) * 1.0) * 6.0; var f = Math.floor(e); var g = e - f * 1.0; var p = b * (1.0 - s); var q = b * (1.0 - s * g); var t = b * (1.0 - (s * (1.0 - g))); switch (f) { case 0 : _r = b; _g = t; _b = p; break; case 1 : _r = q; _g = b; _b = p; break; case 2 : _r = p; _g = b; _b = t; break; case 3 : _r = p; _g = q; _b = b; break; case 4 : _r = t; _g = p; _b = b; break; case 5 : _r = b; _g = p; _b = q; break; } } return 0xff000000 | (Math.floor(_r * 0xff + 0.5) << 16) | (Math.floor(_g * 0xff + 0.5) << 8) | Math.floor(_b * 0xff + 0.5); }; /** * 色の設定. * 引数はコンストラクタと同じ. * void set(...) */ FP.set = function() { apply(this, arguments); }; /** * void setRed(int red) */ FP.setRed = function(v) { this.value |= (v & 0xff) << 16; }; /** * void setGreen(int green) */ FP.setGreen = function(v) { this.value |= (v & 0xff) << 8; }; /** * void setBlue(int blue) */ FP.setBlue = function(v) { this.value |= v & 0xff; }; /** * void setAlpha(int alpha) */ FP.setAlpha = function(v) { this.value |= (v & 0xff) << 24; }; /** * int getRed() */ FP.getRed = function() { return (this.value >>> 16) & 0xff; }; /** * int getGreen() */ FP.getGreen = function() { return (this.value >>> 8) & 0xff; }; /** * int getBlue() */ FP.getBlue = function() { return this.value & 0xff; }; /** * int getAlpha() */ FP.getAlpha = function() { return (this.value >>> 24) & 0xff; }; /** * カラー値をint型で取得. * int getRGB() * これには透明度要素を含むため,純粋にRGB要素を取得したい場合, * 以下のようにする. * int rgba = color.getRGB(); * int rgb = rgba & 0xffffff; * @return 0xAArrGGbb. */ FP.getRGB = function() { return this.value; }; /** * 補色の取得. * @return 補色.但し,透明度要素は常に0xff(不透明). */ FP.getComplementary = function() { return new JColor(~this.value & 0xffffff); }; FP.valueOf = function() { return this.value; }; /** * boolean equals(Object p) */ FP.equals = function(p) { if (p == null || p.constructor !== F) return false; return this.value === p.value; }; /** * String toString() */ FP.toString = function() { return classId + " : " + ("[r=" + this.getRed()) + (",g=" + this.getGreen()) + (",b=" + this.getBlue()) + (",a=" + this.getAlpha()+ "]"); }; var ZEROS = new Array("", "0", "00", "000", "0000", "00000", "000000"); /** * HTMLの色コード文字列にして返す('#'付き). * String toHtmlColor() * @return red = 0x10, green = 0xff, blue = 0x00のとき"#10ff00". */ FP.toHtmlColor = function() { var c = (this.value & 0xffffff).toString(16); return "#" + ZEROS[6 - c.length] + c; }; /** * 各要素を配列にして取得する. * * int[] toArray() * int[] toArray(int[] ary) * @return [r, g, b] * * int[] toArray(boolean hasAlpha) * int[] toArray(boolean hasAlpha, int[] ary) * @param hasAlpha 返す配列に透明度要素を含むかを決める. * 真のとき含める. * @return hasAlphaが真のとき,[r, g, b, a](長さ4), * 偽のとき,[r, g, b](長さ3). */ FP.toArray = function(p0, p1) { switch (arguments.length) { case 0 : return this.toArray(false, new Array(3)); case 1 : if (p0.constructor === Array) { return this.toArray(false, new Array(3)); } else { if (p0) return this.toArray(true, new Array(4)); return this.toArray(false, new Array(3)); } case 2 : p1[0] = (this.value >>> 16) & 0xff; p1[1] = (this.value >>> 8) & 0xff; p1[2] = this.value & 0xff; if (p0) p1[3] = (this.value >>> 24) & 0xff; return p1; } }; } class__JColor__(window);