Sha256: f1dc3f523db700b18d45957ead79342b11dbe73e12ddc924db6ab69bb4a0d6f8

Contents?: true

Size: 614 Bytes

Versions: 15

Compression:

Stored size: 614 Bytes

Contents

# frozen_string_literal: true

module DevSuite
  module Utils
    module Color
      module Strategy
        class Rgb < Base
          def colorize(text, **kwargs)
            r, g, b = kwargs.values_at(:r, :g, :b)

            unless valid_rgb?(r, g, b)
              raise ArgumentError, "RGB values must be integers between 0 and 255"
            end

            "\e[38;2;#{r};#{g};#{b}m#{text}\e[0m"
          end

          private

          def valid_rgb?(r, g, b)
            [r, g, b].all? { |value| value.is_a?(Integer) && value.between?(0, 255) }
          end
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
dev_suite-0.2.13 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.2.12 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.2.11 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.2.10 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.2.9 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.2.8 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.2.7 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.2.6 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.2.5 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.2.4 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.2.3 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.2.2 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.2.1 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.2.0 lib/dev_suite/utils/color/strategy/rgb.rb
dev_suite-0.1.3 lib/dev_suite/utils/color/strategy/rgb.rb