Color class

Represents a color in the RGBA color space. This representation is designed for simplicity of conversion to/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, as well.

Note: this proto does not carry information about the absolute color space that should be used to interpret the RGB value (e.g. sRGB, Adobe RGB, DCI-P3, BT.2020, etc.). By default, applications SHOULD assume the sRGB color space.

Example (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();
  }
  // ...

Example (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;
 }
 // ...

Example (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('');
 };

 // ...
Inheritance
  • Object
  • GeneratedMessage
  • Color

Constructors

Color({double? red, double? green, double? blue, FloatValue? alpha})
factory
Color.fromBuffer(List<int> i, [ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY])
factory
Color.fromJson(String i, [ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY])
factory

Properties

alpha FloatValue
The fraction of this color that should be applied to the pixel. That is, the final pixel color is defined by the equation:
getter/setter pair
blue double
The amount of blue in the color as a value in the interval 0, 1.
getter/setter pair
eventPlugin → EventPlugin?
Subclasses can override this getter to be notified of changes to protobuf fields.
no setterinherited
green double
The amount of green in the color as a value in the interval 0, 1.
getter/setter pair
hashCode int
Calculates a hash code based on the contents of the protobuf.
no setterinherited
info_ → BuilderInfo
no setteroverride
isFrozen bool
Returns true if this message is marked read-only. Otherwise false.
no setterinherited
red double
The amount of red in the color as a value in the interval 0, 1.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
unknownFields → UnknownFieldSet
no setterinherited

Methods

addExtension(Extension extension, Object? value) → void
Adds an extension field value to a repeated field.
inherited
check() → void
inherited
clear() → void
Clears all data that was set in this message.
inherited
clearAlpha() → void
clearBlue() → void
clearExtension(Extension extension) → void
Clears an extension field and also removes the extension.
inherited
clearField(int tagNumber) → void
Clears the contents of a given field.
inherited
clearGreen() → void
clearRed() → void
clone() Color
Creates a deep copy of the fields in this message. (The generated code uses mergeFromMessage.)
override
copyWith(void updates(Color)) Color
Apply updates to a copy of this message.
override
createEmptyInstance() Color
Creates an empty instance of the same message type as this.
override
createMapField<K, V>(int tagNumber, MapFieldInfo<K, V> fi) Map<K, V>
Creates a Map representing a map field.
inherited
createRepeatedField<T>(int tagNumber, FieldInfo<T> fi) List<T>
Creates List implementing a mutable repeated field.
inherited
ensureAlpha() FloatValue
extensionsAreInitialized() bool
inherited
freeze() → GeneratedMessage
Make this message read-only.
inherited
getDefaultForField(int tagNumber) → dynamic
Returns the default value for the given field.
inherited
getExtension(Extension extension) → dynamic
Returns the value of extension.
inherited
getField(int tagNumber) → dynamic
Returns the value of the field associated with tagNumber, or the default value if it is not set.
inherited
getFieldOrNull(int tagNumber) → dynamic
Returns the value of a field, ignoring any defaults.
inherited
getTagNumber(String fieldName) int?
inherited
hasAlpha() bool
hasBlue() bool
hasExtension(Extension extension) bool
Returns true if a value of extension is present.
inherited
hasField(int tagNumber) bool
Whether this message has a field associated with tagNumber.
inherited
hasGreen() bool
hasRed() bool
hasRequiredFields() bool
Whether the message has required fields.
inherited
isInitialized() bool
Whether all required fields in the message and embedded messages are set.
inherited
mergeFromBuffer(List<int> input, [ExtensionRegistry extensionRegistry = ExtensionRegistry.EMPTY]) → void
Merges serialized protocol buffer data into this message.
inherited
mergeFromCodedBufferReader(CodedBufferReader input, [ExtensionRegistry extensionRegistry = ExtensionRegistry.EMPTY]) → void
inherited
mergeFromJson(String data, [ExtensionRegistry extensionRegistry = ExtensionRegistry.EMPTY]) → void
Merges field values from data, a JSON object, encoded as described by GeneratedMessage.writeToJson.
inherited
mergeFromJsonMap(Map<String, dynamic> json, [ExtensionRegistry extensionRegistry = ExtensionRegistry.EMPTY]) → void
Merges field values from a JSON object represented as a Dart map.
inherited
mergeFromMessage(GeneratedMessage other) → void
Merges the contents of the other into this message.
inherited
mergeFromProto3Json(Object? json, {TypeRegistry typeRegistry = const TypeRegistry.empty(), bool ignoreUnknownFields = false, bool supportNamesWithUnderscores = true, bool permissiveEnums = false}) → void
Merges field values from json, a JSON object using proto3 encoding.
inherited
mergeUnknownFields(UnknownFieldSet unknownFieldSet) → void
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setExtension(Extension extension, Object value) → void
Sets the value of a non-repeated extension field to value.
inherited
setField(int tagNumber, Object value) → void
Sets the value of a field by its tagNumber.
inherited
toBuilder() → GeneratedMessage
Creates a writable, shallow copy of this message.
inherited
toDebugString() String
Returns a String representation of this message.
inherited
toProto3Json({TypeRegistry typeRegistry = const TypeRegistry.empty()}) Object?
Returns an Object representing Proto3 JSON serialization of this.
inherited
toString() String
Returns a String representation of this message.
inherited
writeToBuffer() Uint8List
inherited
writeToCodedBufferWriter(CodedBufferWriter output) → void
inherited
writeToJson() String
Returns a JSON string that encodes this message.
inherited
writeToJsonMap() Map<String, dynamic>
Returns the JSON encoding of this message as a Dart Map.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

create() Color
createRepeated() → PbList<Color>
getDefault() Color