Package google.type
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
颜色
表示 RGBA 颜色空间中的一种颜色。此表示法旨在简化各种语言之间的颜色表示法转换,而不是追求紧凑性。例如,此表示法的字段可以轻松提供给 Java 中的 java.awt.Color
构造函数;也可以轻松提供给 iOS 中的 UIColor 的 +colorWithRed:green:blue:alpha
方法;只需稍加处理,便可在 JavaScript 中轻松格式化为 CSS rgba()
字符串。
本参考页面不包含应用于解释 RGB 值的绝对颜色空间(例如 sRGB、Adobe RGB、DCI-P3 和 BT.2020)的相关信息。默认情况下,应用应采用 sRGB 颜色空间。
当需要确定颜色是否相同时,除非另有文档说明,否则实现会将两个颜色视为相同,前提是它们的所有红色、绿色、蓝色和 Alpha 值各自的差值不超过 1e-5
。
示例 (Java):
import com.google.type.Color;
// ...
public static java.awt.Color fromProto(Color protocolor) {
float alpha = protocolor.hasAlpha()
? protocolor.getAlpha().getValue()
: 1.0;
return new java.awt.Color(
protocolor.getRed(),
protocolor.getGreen(),
protocolor.getBlue(),
alpha);
}
public static Color toProto(java.awt.Color color) {
float red = (float) color.getRed();
float green = (float) color.getGreen();
float blue = (float) color.getBlue();
float denominator = 255.0;
Color.Builder resultBuilder =
Color
.newBuilder()
.setRed(red / denominator)
.setGreen(green / denominator)
.setBlue(blue / denominator);
int alpha = color.getAlpha();
if (alpha != 255) {
result.setAlpha(
FloatValue
.newBuilder()
.setValue(((float) alpha) / denominator)
.build());
}
return resultBuilder.build();
}
// ...
示例 (iOS / Obj-C):
// ...
static UIColor* fromProto(Color* protocolor) {
float red = [protocolor red];
float green = [protocolor green];
float blue = [protocolor blue];
FloatValue* alpha_wrapper = [protocolor alpha];
float alpha = 1.0;
if (alpha_wrapper != nil) {
alpha = [alpha_wrapper value];
}
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
static Color* toProto(UIColor* color) {
CGFloat red, green, blue, alpha;
if (![color getRed:&red green:&green blue:&blue alpha:&alpha]) {
return nil;
}
Color* result = [[Color alloc] init];
[result setRed:red];
[result setGreen:green];
[result setBlue:blue];
if (alpha <= 0.9999) {
[result setAlpha:floatWrapperWithValue(alpha)];
}
[result autorelease];
return result;
}
// ...
示例 (JavaScript):
// ...
var protoToCssColor = function(rgb_color) {
var redFrac = rgb_color.red || 0.0;
var greenFrac = rgb_color.green || 0.0;
var blueFrac = rgb_color.blue || 0.0;
var red = Math.floor(redFrac * 255);
var green = Math.floor(greenFrac * 255);
var blue = Math.floor(blueFrac * 255);
if (!('alpha' in rgb_color)) {
return rgbToCssColor(red, green, blue);
}
var alphaFrac = rgb_color.alpha.value || 0.0;
var rgbParams = [red, green, blue].join(',');
return ['rgba(', rgbParams, ',', alphaFrac, ')'].join('');
};
var rgbToCssColor = function(red, green, blue) {
var rgbNumber = new Number((red << 16) | (green << 8) | blue);
var hexString = rgbNumber.toString(16);
var missingZeros = 6 - hexString.length;
var resultBuilder = ['#'];
for (var i = 0; i < missingZeros; i++) {
resultBuilder.push('0');
}
resultBuilder.push(hexString);
return resultBuilder.join('');
};
// ...
字段 |
red |
float
颜色中的红色量,以介于区间 [0, 1] 内的值表示。
|
green |
float
颜色中的绿色量,以介于区间 [0, 1] 内的值表示。
|
blue |
float
颜色中的蓝色量,以介于区间 [0, 1] 内的值表示。
|
alpha |
FloatValue
此颜色在像素中的应用比例。也就是说,最终像素颜色由以下等式定义: pixel color = alpha * (this color) + (1.0 - alpha) * (background color)
也就是说,值为 1.0 表示纯色,而值为 0.0 表示完全透明的颜色。它会使用封装容器消息,而非简单的浮动标量,以便区分默认值和未设置的值。如果省略此字段,此颜色对象将呈现为纯色(就像已明确给定 Alpha 值为 1.0)。
|
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-04-22。
[null,null,["最后更新时间 (UTC):2025-04-22。"],[[["\u003cp\u003e\u003ccode\u003eColor\u003c/code\u003e represents a color in the RGBA color space, designed for easy conversion between color representations in different programming languages.\u003c/p\u003e\n"],["\u003cp\u003eApplications should assume the sRGB color space when interpreting the RGB values unless otherwise specified.\u003c/p\u003e\n"],["\u003cp\u003eTwo colors are considered equal if their red, green, blue, and alpha values differ by at most 1e-5.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ealpha\u003c/code\u003e field, if omitted, defaults to 1.0, representing a solid color.\u003c/p\u003e\n"],["\u003cp\u003eCode examples demonstrate how to convert \u003ccode\u003eColor\u003c/code\u003e to and from native color objects in Java, iOS/Obj-C, and JavaScript.\u003c/p\u003e\n"]]],["The core content describes the `Color` message, representing colors in the RGBA space. It uses `red`, `green`, and `blue` fields (float values from 0 to 1) and an optional `alpha` field (float, 0.0 to 1.0, 1.0 being solid). Implementations treat colors as equal if their RGBA values differ by at most 1e-5. Examples demonstrate conversions between this color representation and Java, iOS, and JavaScript color formats. The color space defaults to sRGB.\n"],null,["# Package google.type\n\nIndex\n-----\n\n- [Color](/workspace/add-ons/reference/rpc/google.type#google.type.Color) (message)\n\nColor\n-----\n\nRepresents a color in the RGBA color space. This representation is designed for simplicity of conversion to and from color representations in various languages over compactness. For example, the fields of this representation can be trivially provided to the constructor of `java.awt.Color` in Java; it can also be trivially provided to UIColor's `+colorWithRed:green:blue:alpha` method in iOS; and, with just a little work, it can be easily formatted into a CSS `rgba()` string in JavaScript.\n\nThis reference page doesn't have information about the absolute color space that should be used to interpret the RGB value---for example, sRGB, Adobe RGB, DCI-P3, and BT.2020. By default, applications should assume the sRGB color space.\n\nWhen color equality needs to be decided, implementations, unless documented otherwise, treat two colors as equal if all their red, green, blue, and alpha values each differ by at most `1e-5`.\n\nExample (Java): \n\n import com.google.type.Color;\n\n // ...\n public static java.awt.Color fromProto(Color protocolor) {\n float alpha = protocolor.hasAlpha()\n ? protocolor.getAlpha().getValue()\n : 1.0;\n\n return new java.awt.Color(\n protocolor.getRed(),\n protocolor.getGreen(),\n protocolor.getBlue(),\n alpha);\n }\n\n public static Color toProto(java.awt.Color color) {\n float red = (float) color.getRed();\n float green = (float) color.getGreen();\n float blue = (float) color.getBlue();\n float denominator = 255.0;\n Color.Builder resultBuilder =\n Color\n .newBuilder()\n .setRed(red / denominator)\n .setGreen(green / denominator)\n .setBlue(blue / denominator);\n int alpha = color.getAlpha();\n if (alpha != 255) {\n result.setAlpha(\n FloatValue\n .newBuilder()\n .setValue(((float) alpha) / denominator)\n .build());\n }\n return resultBuilder.build();\n }\n // ...\n\nExample (iOS / Obj-C): \n\n // ...\n static UIColor* fromProto(Color* protocolor) {\n float red = [protocolor red];\n float green = [protocolor green];\n float blue = [protocolor blue];\n FloatValue* alpha_wrapper = [protocolor alpha];\n float alpha = 1.0;\n if (alpha_wrapper != nil) {\n alpha = [alpha_wrapper value];\n }\n return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];\n }\n\n static Color* toProto(UIColor* color) {\n CGFloat red, green, blue, alpha;\n if (![color getRed:&red green:&green blue:&blue alpha:&alpha]) {\n return nil;\n }\n Color* result = [[Color alloc] init];\n [result setRed:red];\n [result setGreen:green];\n [result setBlue:blue];\n if (alpha \u003c= 0.9999) {\n [result setAlpha:floatWrapperWithValue(alpha)];\n }\n [result autorelease];\n return result;\n }\n // ...\n\nExample (JavaScript): \n\n // ...\n\n var protoToCssColor = function(rgb_color) {\n var redFrac = rgb_color.red || 0.0;\n var greenFrac = rgb_color.green || 0.0;\n var blueFrac = rgb_color.blue || 0.0;\n var red = Math.floor(redFrac * 255);\n var green = Math.floor(greenFrac * 255);\n var blue = Math.floor(blueFrac * 255);\n\n if (!('alpha' in rgb_color)) {\n return rgbToCssColor(red, green, blue);\n }\n\n var alphaFrac = rgb_color.alpha.value || 0.0;\n var rgbParams = [red, green, blue].join(',');\n return ['rgba(', rgbParams, ',', alphaFrac, ')'].join('');\n };\n\n var rgbToCssColor = function(red, green, blue) {\n var rgbNumber = new Number((red \u003c\u003c 16) | (green \u003c\u003c 8) | blue);\n var hexString = rgbNumber.toString(16);\n var missingZeros = 6 - hexString.length;\n var resultBuilder = ['#'];\n for (var i = 0; i \u003c missingZeros; i++) {\n resultBuilder.push('0');\n }\n resultBuilder.push(hexString);\n return resultBuilder.join('');\n };\n\n // ...\n\n| Fields ||\n|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `red` | `float` The amount of red in the color as a value in the interval \\[0, 1\\]. |\n| `green` | `float` The amount of green in the color as a value in the interval \\[0, 1\\]. |\n| `blue` | `float` The amount of blue in the color as a value in the interval \\[0, 1\\]. |\n| `alpha` | [FloatValue](https://protobuf.dev/reference/protobuf/google.protobuf/#float-value) The fraction of this color that should be applied to the pixel. That is, the final pixel color is defined by the equation: `pixel color = alpha * (this color) + (1.0 - alpha) * (background color)` This means that a value of 1.0 corresponds to a solid color, whereas a value of 0.0 corresponds to a completely transparent color. This uses a wrapper message rather than a simple float scalar so that it is possible to distinguish between a default value and the value being unset. If omitted, this color object is rendered as a solid color (as if the alpha value had been explicitly given a value of 1.0). |"]]