當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Processing color()用法及代碼示例

Processing, color()用法介紹。

用法

  • color(gray)
  • color(gray, alpha)
  • color(v1, v2, v3)
  • color(v1, v2, v3, alpha)

參數

  • gray (int) 數字指定白色和黑色之間的值
  • alpha (int, float) 相對於當前顏色範圍
  • v1 (int, float) 相對於當前顏色範圍的紅色或色調值
  • v2 (int, float) 相對於當前顏色範圍的綠色或飽和度值
  • v3 (int, float) 相對於當前顏色範圍的藍色或亮度值

返回

  • int

說明

創建顏色以存儲在 color 數據類型的變量中。根據當前的 colorMode() ,參數被解釋為 RGB 或 HSB 值。默認模式是從 0 到 255 的 RGB 值,因此,color(255, 204, 0) 將返回亮黃色(參見上麵的第一個示例)。



請注意,如果僅向 color() 提供一個值,它將被解釋為灰度值。添加第二個值,它將用於 Alpha 透明度。當指定三個值時,它們被解釋為 RGB 或 HSB 值。添加第四個值會應用 Alpha 透明度。



請注意,使用十六進製表示法時,不必使用 color() ,如:color c = #006699



有關如何存儲顏色的更多信息,請參閱color 數據類型的參考。

例子

size(400,400);
color c = color(255, 204, 0);  // Define color 'c'
fill(c);  // Use color variable 'c' as fill color
noStroke();  // Don't draw a stroke around shapes
rect(120, 80, 220, 220);  // Draw rectangle
Image output for example 1
size(400,400);
color c = color(255, 204, 0);  // Define color 'c'
fill(c);  // Use color variable 'c' as fill color
noStroke();  // Don't draw a stroke around shapes
ellipse(100, 100, 320, 320);  // Draw left circle

// Using only one value with color()
// generates a grayscale value.
c = color(65);  // Update 'c' with grayscale value
fill(c);  // Use updated 'c' as fill color
ellipse(300, 300, 320, 320);  // Draw right circle
Image output for example 2
size(400,400);
color c;  // Declare color 'c'
noStroke();  // Don't draw a stroke around shapes

// If no colorMode is specified, then the
// default of RGB with scale of 0-255 is used.
c = color(50, 55, 100);  // Create a color for 'c'
fill(c);  // Use color variable 'c' as fill color
rect(0, 40, 180, 320);  // Draw left rect

colorMode(HSB, 100);  // Use HSB with scale of 0-100
c = color(50, 55, 100);  // Update 'c' with new color
fill(c);  // Use updated 'c' as fill color
rect(220, 40, 180, 320);  // Draw right rect
Image output for example 3

有關的

相關用法


注:本文由純淨天空篩選整理自processing.org大神的英文原創作品 color()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。