Color Class
Color クラスは、色を表現するためのクラスです。sRGB カラースペースを基準として色を管理し、様々な形式での色の設定・取得をサポートします。
基本的な使い方
Section titled “基本的な使い方”Color クラスは、メソッドチェーンを使用して色を設定します。コンストラクタは引数を取らず、setRGB()、setHex()、setStyle() のいずれかのメソッドで色を設定します。
import { Color } from "@navaramap/three";
// RGB値で設定(各成分は 0.0〜1.0)const red = new Color().setRGB(1.0, 0.0, 0.0);
// 16進数で設定const green = new Color().setHex(0x00ff00);
// CSS カラー文字列で設定const blue = new Color().setStyle("#0000ff");Methods
Section titled “Methods”setRGB()
Section titled “setRGB()”RGB 値で色を設定します(sRGB カラースペース)。
Syntax:
setRGB(r: number, g: number, b: number): thisParameters:
r: 赤成分(0.0〜1.0)g: 緑成分(0.0〜1.0)b: 青成分(0.0〜1.0)
Returns:
メソッドチェーン用に自身のインスタンスを返します。
Example:
// 純粋な赤const red = new Color().setRGB(1.0, 0.0, 0.0);
// オレンジconst orange = new Color().setRGB(1.0, 0.5, 0.0);
// グレー(50%)const gray = new Color().setRGB(0.5, 0.5, 0.5);setRGBLinear()
Section titled “setRGBLinear()”RGB 値で色を設定します(リニアカラースペース、ガンマ補正なし)。
Syntax:
setRGBLinear(r: number, g: number, b: number): thisParameters:
r: 赤成分(0.0〜1.0)g: 緑成分(0.0〜1.0)b: 青成分(0.0〜1.0)
Returns:
メソッドチェーン用に自身のインスタンスを返します。
Example:
// リニアカラースペースで色を設定const linearColor = new Color().setRGBLinear(0.5, 0.5, 0.5);setHex()
Section titled “setHex()”16進数で色を設定します(sRGB カラースペース)。
Syntax:
setHex(hex: number): thisParameters:
hex: 16進数のカラー値(例:0xff0000は赤)
Returns:
メソッドチェーン用に自身のインスタンスを返します。
Example:
// 赤const red = new Color().setHex(0xff0000);
// 緑const green = new Color().setHex(0x00ff00);
// 青const blue = new Color().setHex(0x0000ff);
// 白const white = new Color().setHex(0xffffff);
// 黒const black = new Color().setHex(0x000000);setStyle()
Section titled “setStyle()”CSS カラー文字列で色を設定します(sRGB カラースペース)。
Syntax:
setStyle(style: string): thisParameters:
style: CSS カラー文字列
Returns:
メソッドチェーン用に自身のインスタンスを返します。
Supported formats:
- 16進数:
"#ff0000","#f00" - RGB:
"rgb(255, 0, 0)" - RGBA:
"rgba(255, 0, 0, 1)" - HSL:
"hsl(0, 100%, 50%)" - 名前付きカラー:
"red","blue","green"など
Example:
// 16進数形式const red = new Color().setStyle("#ff0000");const shortRed = new Color().setStyle("#f00");
// RGB形式const green = new Color().setStyle("rgb(0, 255, 0)");
// HSL形式const blue = new Color().setStyle("hsl(240, 100%, 50%)");
// 名前付きカラーconst coral = new Color().setStyle("coral");copy()
Section titled “copy()”この色の値を別の Color インスタンスにコピーします。
Syntax:
copy(color: Color): thisParameters:
color: コピー先の Color インスタンス
Returns:
コピーされた値を持つターゲット Color インスタンス
Example:
const original = new Color().setHex(0xff0000);const target = new Color();
original.copy(target);// target は original と同じ色になるclone()
Section titled “clone()”同じ値を持つ新しい Color インスタンスを作成します。
Syntax:
clone(): ColorReturns:
新しい Color インスタンス
Example:
const original = new Color().setHex(0xff0000);const cloned = original.clone();
// original と cloned は同じ色だが、別のインスタンスtoArray()
Section titled “toArray()”色を RGB 配列として返します。
Syntax:
toArray(): [r: number, g: number, b: number]Returns:
[red, green, blue] の値(各成分は 0.0〜1.0)
Example:
const color = new Color().setHex(0xff8000);const [r, g, b] = color.toArray();// r ≈ 1.0, g ≈ 0.5, b = 0.0toHex()
Section titled “toHex()”色を16進数として返します。
Syntax:
toHex(): numberReturns:
16進数のカラー値(例: 0xff0000)
Example:
const color = new Color().setStyle("#ff8000");const hex = color.toHex();// hex = 0xff8000srgb()
Section titled “srgb()”リニアカラースペースから sRGB カラースペースに変換した新しい Color インスタンスを返します。
Syntax:
srgb(): ColorReturns:
sRGB カラースペースに変換された新しい Color インスタンス
Example:
const linearColor = new Color().setRGBLinear(0.5, 0.5, 0.5);const srgbColor = linearColor.srgb();Properties
Section titled “Properties”Type: THREE.Color(読み取り専用)
Description: 内部で使用している Three.js の Color インスタンスへのアクセスを提供します。
この プロパティは内部実装の詳細であり、通常の使用では必要ありません。Three.js との直接的な統合が必要な場合にのみ使用してください。
マテリアルでの使用
Section titled “マテリアルでの使用”import ThreeView, { Color } from "@navaramap/three";
const view = new ThreeView();await view.init();
// 地球の基本色を設定view.globe.color = new Color().setStyle("#1a1a2e");ライトでの使用
Section titled “ライトでの使用”import ThreeView, { Color } from "@navaramap/three";import { SunLightDesc } from "@navaramap/three-default-descs";
const view = new ThreeView();await view.init();
// 太陽光の色を設定view.addLight<SunLightDesc>({ sun: { color: new Color().setHex(0xffffff), intensity: 3, },});ColorMap での使用
Section titled “ColorMap での使用”import { ColorMap, Color } from "@navaramap/three";
// ref: https://colorbrewer2.org/#type=sequential&scheme=YlGnBu&n=9const ylGnBu = new ColorMap("sequential", "YlGnBu", [ new Color().setStyle("#ffffd9"), new Color().setStyle("#edf8b1"), new Color().setStyle("#c7e9b4"), new Color().setStyle("#7fcdbb"), new Color().setStyle("#41b6c4"), new Color().setStyle("#1d91c0"), new Color().setStyle("#225ea8"), new Color().setStyle("#253494"), new Color().setStyle("#081d58"),]);- ColorMap クラス - カラーグラデーションの定義
- Globe クラス -
colorプロパティ - SunLightDesc - ライトの色設定
- AmbientLightDesc - 環境光の色設定