Sha256: 00fc6122d6dfbacb7f4d65dafb84ed4301b81ff93826f81ea3f9c7d0ac9778ed

Contents?: true

Size: 1.77 KB

Versions: 20

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module Faker
  class Color < Base
    class << self
      ##
      # Produces a hex color code.
      #
      # @return [String]
      #
      # @example
      #   Faker::Color.hex_color #=> "#31a785"
      #
      # @faker.version 1.5.0
      def hex_color
        format('#%06x', (rand * 0xffffff))
      end

      ##
      # Produces the name of a color.
      #
      # @return [String]
      #
      # @example
      #   Faker::Color.color_name #=> "yellow"
      #
      # @faker.version 1.6.2
      def color_name
        fetch('color.name')
      end

      # @private
      def single_rgb_color
        sample((0..255).to_a)
      end

      ##
      # Produces an array of integers representing an RGB color.
      #
      # @return [Array(Integer, Integer, Integer)]
      #
      # @example
      #   Faker::Color.rgb_color #=> [54, 233, 67]
      #
      # @faker.version 1.5.0
      def rgb_color
        Array.new(3) { single_rgb_color }
      end

      ##
      # Produces an array of floats representing an HSL color.
      # The array is in the form of `[hue, saturation, lightness]`.
      #
      # @return [Array(Float, Float, Float)]
      #
      # @example
      #   Faker::Color.hsl_color #=> [69.87, 0.66, 0.3]
      #
      # @faker.version 1.5.0
      def hsl_color
        [sample((0..360).to_a), rand.round(2), rand.round(2)]
      end

      ##
      # Produces an array of floats representing an HSLA color.
      # The array is in the form of `[hue, saturation, lightness, alpha]`.
      #
      # @return [Array(Float, Float, Float, Float)]
      #
      # @example
      #   Faker::Color.hsla_color #=> [154.77, 0.36, 0.9, 0.2]
      #
      # @faker.version 1.5.0
      def hsla_color
        hsl_color << rand.round(1)
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
faker-2.23.0 lib/faker/default/color.rb
faker-2.22.0 lib/faker/default/color.rb
faker-2.21.0 lib/faker/default/color.rb
faker-2.20.0 lib/faker/default/color.rb
faker-2.19.0 lib/faker/default/color.rb
logstash-output-scalyr-0.1.7 vendor/bundle/jruby/2.5.0/bundler/gems/faker-e1bd4a5a5777/lib/faker/default/color.rb
faker-2.18.0 lib/faker/default/color.rb
faker-2.17.0 lib/faker/default/color.rb
faker-2.16.0 lib/faker/default/color.rb
faker-2.15.1 lib/faker/default/color.rb
faker-2.15.0 lib/faker/default/color.rb
faker-2.14.0 lib/faker/default/color.rb
faker-2.13.0 lib/faker/default/color.rb
faker-2.12.0 lib/faker/default/color.rb
faker-2.11.0 lib/faker/default/color.rb
faker-2.10.2 lib/faker/default/color.rb
faker-2.10.1 lib/faker/default/color.rb
faker-2.10.0 lib/faker/default/color.rb
faker-2.9.0 lib/faker/default/color.rb
faker-2.8.1 lib/faker/default/color.rb