Sha256: ccb3da9a822c4c2eec5b15e1dd7180b4c2c9e9618700de851ba8a32943477c01

Contents?: true

Size: 1013 Bytes

Versions: 1

Compression:

Stored size: 1013 Bytes

Contents

# frozen_string_literal: true

require 'blurhash/version'
require 'blurhash_ext'

module Blurhash
  def self.encode(width, height, pixels, x_comp: 4, y_comp: 3)
    p = pixels.pack("C#{pixels.size}")
    return Unstable.blurHashForPixels(x_comp, y_comp, width, height, p)
  end

  def self.components(str)
    size_flag = Base83.decode83(str[0])
    y_comp    = (size_flag / 9) + 1
    x_comp    = (size_flag % 9) + 1

    return if str.size != 4 + 2 * x_comp * y_comp

    [x_comp, y_comp]
  end

  module Base83
    DIGIT_CHARACTERS = %w(
      0 1 2 3 4 5 6 7 8 9
      A B C D E F G H I J
      K L M N O P Q R S T
      U V W X Y Z a b c d
      e f g h i j k l m n
      o p q r s t u v w x
      y z # $ % * + , - .
      : ; = ? @ [ ] ^ _ {
      | } ~
    ).freeze

    def self.decode83(str)
      value = 0

      str.each_char.with_index do |c, i|
        digit = DIGIT_CHARACTERS.find_index(c)
        value = value * 83 + digit
      end

      value
    end
  end

  private_constant :Unstable
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
blurhash-0.1.7 lib/blurhash.rb