<commit_before>module RGB
  class Color
    attr_reader :hue, :saturation, :lightness # HSL

    def self.from_rgb_hex(color)
      color = "#%.6x" % color if color.is_a? Integer
      rgb = color[1,7].scan(/.{2}/).map{ |component| component.to_i(16) }
      from_rgb(*rgb)
    end

    def self.from_rgb(*rgb)
      rgb.map!{ |c| c / 255.0 }
      min_rgb = rgb.min
      max_rgb = rgb.max
      delta = max_rgb - min_rgb

      lightness = (max_rgb + min_rgb) / 2.0

      if delta < 1e-5
        hue = 0
        saturation = 0
      else
        saturation = if lightness < 0.5
          delta / (max_rgb + min_rgb)
        else
          delta / (2 - max_rgb - min_rgb)
        end

        deltas = rgb.map{ |c| (((max_rgb - c) / 6.0) + (delta / 2.0)) / delta }

        hue = if (rgb[0] - max_rgb).abs < 1e-5
          deltas[2] - deltas[1]
        elsif (rgb[1] - max_rgb).abs < 1e-5
          (1.0 / 3.0) + deltas[0] - deltas[2]
        else
          (2.0 / 3.0) + deltas[1] - deltas[0]
        end

        hue += 1 if hue < 0
        hue -= 1 if hue > 1
      end
      from_fractions(hue, saturation, lightness)
    end

    def self.from_fractions(hue, saturation, lightness)
      new(360 * hue, saturation, lightness)
    end

    def initialize(*hsl)
      self.hue, self.saturation, self.lightness = hsl
    end

    def to_rgb
      m2 = lightness <= 0.5 ? lightness * (saturation + 1) : lightness + saturation - lightness * saturation
      m1 = lightness * 2 - m2
      [hue_to_rgb(m1, m2, hp + 1.0/3), hue_to_rgb(m1, m2, hp), hue_to_rgb(m1, m2, hp - 1.0/3)].map { |c| (c * 0xff).round }
    end

    def to_hsl
      [hue, saturation, lightness]
    end

    def to_rgb_hex
      "#" + to_rgb.map {|c| "%02X" % c }.join
    end

    def hue=(value)
      @hue = value % 360
    end

    def saturation=(value)
      @saturation = if value < 0
        0.0
      elsif value > 1
        1.0
      else
        value
      end
    end

    def lightness=(value)
      @lightness = if value < 0
        0.0
      elsif value > 1
        1.0
      else
        value
      end
    end

    def lighten!(amount)
      @lightness += amount / 100.0
    end

    def lighten_percent!(percentage)
      @lightness += (1 - @lightness) * (percentage / 100.0)
    end

    def darken!(amount)
      @lightness -= (amount / 100.0)
      @lightness = 0 if @lightness < 0
      @lightness
    end

    def darken_percent!(percentage)
      @lightness *= 1.0 - (percentage / 100.0)
    end

    def saturate!(amount)
      @saturation += amount / 100.0
    end

    def saturate_percent!(percentage)
      @saturation += (1 - @saturation) * (percentage / 100.0)
    end

    def desaturate!(amount)
      @saturation -= amount / 100.0
    end

    def desaturate_percent!(percentage)
      @saturation *= (1.0 - (percentage / 100.0))
    end

    # define non-bang methods
    [:darken, :darken_percent, :lighten, :lighten_percent, :saturate, :saturate_percent, :desaturate,
      :desaturate_percent].each do |method_name|
        define_method method_name do |*args|
          dup.tap { |color| color.send(:"#{method_name}!", *args) }
        end
    end

  private
    # hue as a percentage
    def hp
      hue / 360.0
    end

    # helper for making rgb
    def hue_to_rgb(m1, m2, h)
      h += 1 if h < 0
      h -= 1 if h > 1
      return m1 + (m2 - m1) * h * 6 if h * 6 < 1
      return m2 if h * 2 < 1
      return m1 + (m2 - m1) * (2.0/3 - h) * 6 if h * 3 < 2
      return m1
    end
  end
end<commit_msg>Reafctoring<commit_after>module RGB
  class Color
    attr_reader :hue, :saturation, :lightness # HSL

    def self.from_rgb_hex(color)
      color = "#%.6x" % color if color.is_a? Integer
      rgb = color[1,7].scan(/.{2}/).map{ |component| component.to_i(16) }
      from_rgb(*rgb)
    end

    def self.calc_saturation(max_rgb, min_rgb, delta, lightness)
      if lightness < 0.5
        delta / (max_rgb + min_rgb)
      else
        delta / (2 - max_rgb - min_rgb)
      end
    end

    def self.from_rgb(*rgb)
      rgb.map!{ |c| c / 255.0 }
      min_rgb, max_rgb = rgb.min, rgb.max
      delta = max_rgb - min_rgb

      lightness = (max_rgb + min_rgb) / 2.0

      if delta < 1e-5
        hue = saturation = 0
      else
        saturation = calc_saturation(max_rgb, min_rgb, delta, lightness)
        deltas = rgb.map{ |c| (((max_rgb - c) / 6.0) + (delta / 2.0)) / delta }

        hue = if (rgb[0] - max_rgb).abs < 1e-5
          deltas[2] - deltas[1]
        elsif (rgb[1] - max_rgb).abs < 1e-5
          (1.0 / 3.0) + deltas[0] - deltas[2]
        else
          (2.0 / 3.0) + deltas[1] - deltas[0]
        end

        hue += 1 if hue < 0
        hue -= 1 if hue > 1
      end
      from_fractions(hue, saturation, lightness)
    end

    def self.from_fractions(hue, saturation, lightness)
      new(360 * hue, saturation, lightness)
    end

    def initialize(*hsl)
      self.hue, self.saturation, self.lightness = hsl
    end

    def to_rgb
      m2 = if lightness <= 0.5
        lightness * (saturation + 1)
      else
        lightness + saturation - lightness * saturation
      end

      m1 = lightness * 2 - m2

      [
        hue_to_rgb(m1, m2, hue_percentage + 1.0 / 3),
        hue_to_rgb(m1, m2, hue_percentage),
        hue_to_rgb(m1, m2, hue_percentage - 1.0 / 3)
      ].map { |c| (c * 0xff).round }
    end

    def to_hsl
      [hue, saturation, lightness]
    end

    def to_rgb_hex
      "#" + to_rgb.map {|c| "%02X" % c }.join
    end

    def hue=(value)
      @hue = value % 360
    end

    def saturation=(value)
      @saturation = if value < 0
        0.0
      elsif value > 1
        1.0
      else
        value
      end
    end

    def lightness=(value)
      @lightness = if value < 0
        0.0
      elsif value > 1
        1.0
      else
        value
      end
    end

    def lighten!(amount)
      @lightness += amount / 100.0
    end

    def lighten_percent!(percentage)
      @lightness += (1 - @lightness) * (percentage / 100.0)
    end

    def darken!(amount)
      @lightness -= (amount / 100.0)
      @lightness = 0 if @lightness < 0
      @lightness
    end

    def darken_percent!(percentage)
      @lightness *= 1.0 - (percentage / 100.0)
    end

    def saturate!(amount)
      @saturation += amount / 100.0
    end

    def saturate_percent!(percentage)
      @saturation += (1 - @saturation) * (percentage / 100.0)
    end

    def desaturate!(amount)
      @saturation -= amount / 100.0
    end

    def desaturate_percent!(percentage)
      @saturation *= (1.0 - (percentage / 100.0))
    end

    # define non-bang methods
    [:darken, :darken_percent, :lighten, :lighten_percent, :saturate, :saturate_percent, :desaturate,
      :desaturate_percent].each do |method_name|
        define_method method_name do |*args|
          dup.tap { |color| color.send(:"#{method_name}!", *args) }
        end
    end

  private
    def hue_percentage
      hue / 360.0
    end

    # helper for making rgb
    def hue_to_rgb(m1, m2, h)
      h += 1 if h < 0
      h -= 1 if h > 1
      return m1 + (m2 - m1) * h * 6 if h * 6 < 1
      return m2 if h * 2 < 1
      return m1 + (m2 - m1) * (2.0/3 - h) * 6 if h * 3 < 2
      return m1
    end
  end
end
{'fraction_non_alphanumeric': 0.12304930258251623, 'fraction_numerical': 0.042949868802651565, 'mean_word_length': 1.7515197568389058, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7439178', 'n_tokens_mistral': 2932, 'n_tokens_neox': 2667, 'n_words': 702}