Class: Gcloud::Vision::Annotation::Properties::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/vision/annotation/properties.rb

Overview

# Color

Color information consisting of RGB channels, score, and fraction of image the color occupies in the image.

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

image = vision.image "path/to/logo.jpg"
properties = image.properties

color = properties.colors.first
color.red #=> 247.0
color.green #=> 236.0
color.blue #=> 20.0
color.rgb #=> "f7ec14"
color.alpha #=> 1.0
color.score #=> 0.20301804
color.pixel_fraction #=> 0.0072649573

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeColor

Returns a new instance of Color.



143
144
145
# File 'lib/gcloud/vision/annotation/properties.rb', line 143

def initialize
  @gapi = {}
end

Instance Attribute Details

#gapiObject



139
140
141
# File 'lib/gcloud/vision/annotation/properties.rb', line 139

def gapi
  @gapi
end

Class Method Details

.from_gapi(gapi) ⇒ Object



238
239
240
# File 'lib/gcloud/vision/annotation/properties.rb', line 238

def self.from_gapi gapi
  new.tap { |f| f.instance_variable_set :@gapi, gapi }
end

Instance Method Details

#alphaFloat

The amount this color that should be applied to the pixel. A value of 1.0 corresponds to a solid color, whereas a value of 0.0 corresponds to a completely transparent color.

Returns:

  • (Float)

    A value in the range [0, 1].



181
182
183
# File 'lib/gcloud/vision/annotation/properties.rb', line 181

def alpha
  @gapi["color"]["alpha"] || 1.0
end

#blueFloat

The amount of blue in the color.

Returns:

  • (Float)

    A value in the interval [0, 255].



170
171
172
# File 'lib/gcloud/vision/annotation/properties.rb', line 170

def blue
  @gapi["color"]["blue"]
end

#greenFloat

The amount of green in the color.

Returns:

  • (Float)

    A value in the interval [0, 255].



161
162
163
# File 'lib/gcloud/vision/annotation/properties.rb', line 161

def green
  @gapi["color"]["green"]
end

#inspectObject



232
233
234
# File 'lib/gcloud/vision/annotation/properties.rb', line 232

def inspect
  "#<Color #{self}>"
end

#pixel_fractionFloat

Stores the fraction of pixels the color occupies in the image.

Returns:

  • (Float)

    A value in the range [0, 1].



205
206
207
# File 'lib/gcloud/vision/annotation/properties.rb', line 205

def pixel_fraction
  @gapi["pixelFraction"]
end

#redFloat

The amount of red in the color.

Returns:

  • (Float)

    A value in the interval [0, 255].



152
153
154
# File 'lib/gcloud/vision/annotation/properties.rb', line 152

def red
  @gapi["color"]["red"]
end

#rgbObject



185
186
187
188
189
# File 'lib/gcloud/vision/annotation/properties.rb', line 185

def rgb
  red.to_i.to_s(16).rjust(2, "0") +
    green.to_i.to_s(16).rjust(2, "0") +
    blue.to_i.to_s(16).rjust(2, "0")
end

#scoreFloat

Image-specific score for this color.

Returns:

  • (Float)

    A value in the range [0, 1].



196
197
198
# File 'lib/gcloud/vision/annotation/properties.rb', line 196

def score
  @gapi["score"]
end

#to_hHash

Converts object to a hash. All keys will be symbolized.

Returns:

  • (Hash)


214
215
216
# File 'lib/gcloud/vision/annotation/properties.rb', line 214

def to_h
  to_hash
end

#to_hashHash

Converts object to a hash. All keys will be symbolized.

Returns:

  • (Hash)


223
224
225
226
# File 'lib/gcloud/vision/annotation/properties.rb', line 223

def to_hash
  { red: red, green: green, blue: blue, alpha: alpha, rgb: rgb,
    score: score, pixel_fraction: pixel_fraction }
end

#to_sObject



228
229
230
# File 'lib/gcloud/vision/annotation/properties.rb', line 228

def to_s
  "(colors: #{rgb})"
end