You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.5 KiB

  1. # frozen_string_literal: true
  2. require 'mime/types/columnar'
  3. module Paperclip
  4. class ColorExtractor < Paperclip::Processor
  5. MIN_CONTRAST = 3.0
  6. FREQUENCY_THRESHOLD = 0.01
  7. def make
  8. depth = 8
  9. # Determine background palette by getting colors close to the image's edge only
  10. background_palette = palette_from_histogram(convert(':source -alpha set -gravity Center -region 75%x75% -fill None -colorize 100% -alpha transparent +region -format %c -colors :quantity -depth :depth histogram:info:', source: File.expand_path(@file.path), quantity: 10, depth: depth), 10)
  11. # Determine foreground palette from the whole image
  12. foreground_palette = palette_from_histogram(convert(':source -format %c -colors :quantity -depth :depth histogram:info:', source: File.expand_path(@file.path), quantity: 10, depth: depth), 10)
  13. background_color = background_palette.first || foreground_palette.first
  14. foreground_colors = []
  15. return @file if background_color.nil?
  16. max_distance = 0
  17. max_distance_color = nil
  18. foreground_palette.each do |color|
  19. distance = ColorDiff.between(background_color, color)
  20. if distance > max_distance
  21. max_distance = distance
  22. max_distance_color = color
  23. end
  24. end
  25. foreground_colors << max_distance_color unless max_distance_color.nil?
  26. max_distance = 0
  27. max_distance_color = nil
  28. foreground_palette.each do |color|
  29. distance = ColorDiff.between(background_color, color)
  30. contrast = w3c_contrast(background_color, color)
  31. if distance > max_distance && contrast >= MIN_CONTRAST && !foreground_colors.include?(color)
  32. max_distance = distance
  33. max_distance_color = color
  34. end
  35. end
  36. foreground_colors << max_distance_color unless max_distance_color.nil?
  37. # If we don't have enough colors for accent and foreground, generate
  38. # new ones by manipulating the background color
  39. (2 - foreground_colors.size).times do |i|
  40. foreground_colors << lighten_or_darken(background_color, 35 + (15 * i))
  41. end
  42. # We want the color with the highest contrast to background to be the foreground one,
  43. # and the one with the highest saturation to be the accent one
  44. foreground_color = foreground_colors.max_by { |rgb| w3c_contrast(background_color, rgb) }
  45. accent_color = foreground_colors.max_by { |rgb| rgb_to_hsl(rgb.r, rgb.g, rgb.b)[1] }
  46. meta = {
  47. colors: {
  48. background: rgb_to_hex(background_color),
  49. foreground: rgb_to_hex(foreground_color),
  50. accent: rgb_to_hex(accent_color),
  51. },
  52. }
  53. attachment.instance.file.instance_write(:meta, (attachment.instance.file.instance_read(:meta) || {}).merge(meta))
  54. @file
  55. end
  56. private
  57. def w3c_contrast(color1, color2)
  58. luminance1 = (0.2126 * color1.r + 0.7152 * color1.g + 0.0722 * color1.b) + 0.05
  59. luminance2 = (0.2126 * color2.r + 0.7152 * color2.g + 0.0722 * color2.b) + 0.05
  60. if luminance1 > luminance2
  61. luminance1 / luminance2
  62. else
  63. luminance2 / luminance1
  64. end
  65. end
  66. # rubocop:disable Style/MethodParameterName
  67. def rgb_to_hsl(r, g, b)
  68. r /= 255.0
  69. g /= 255.0
  70. b /= 255.0
  71. max = [r, g, b].max
  72. min = [r, g, b].min
  73. h = (max + min) / 2.0
  74. s = (max + min) / 2.0
  75. l = (max + min) / 2.0
  76. if max == min
  77. h = 0
  78. s = 0 # achromatic
  79. else
  80. d = max - min
  81. s = l >= 0.5 ? d / (2.0 - max - min) : d / (max + min)
  82. case max
  83. when r
  84. h = (g - b) / d + (g < b ? 6.0 : 0)
  85. when g
  86. h = (b - r) / d + 2.0
  87. when b
  88. h = (r - g) / d + 4.0
  89. end
  90. h /= 6.0
  91. end
  92. [(h * 360).round, (s * 100).round, (l * 100).round]
  93. end
  94. def hue_to_rgb(p, q, t)
  95. t += 1 if t.negative?
  96. t -= 1 if t > 1
  97. return (p + (q - p) * 6 * t) if t < 1 / 6.0
  98. return q if t < 1 / 2.0
  99. return (p + (q - p) * (2 / 3.0 - t) * 6) if t < 2 / 3.0
  100. p
  101. end
  102. def hsl_to_rgb(h, s, l)
  103. h /= 360.0
  104. s /= 100.0
  105. l /= 100.0
  106. r = 0.0
  107. g = 0.0
  108. b = 0.0
  109. if s == 0.0
  110. r = l.to_f
  111. g = l.to_f
  112. b = l.to_f # achromatic
  113. else
  114. q = l < 0.5 ? l * (1 + s) : l + s - l * s
  115. p = 2 * l - q
  116. r = hue_to_rgb(p, q, h + 1 / 3.0)
  117. g = hue_to_rgb(p, q, h)
  118. b = hue_to_rgb(p, q, h - 1 / 3.0)
  119. end
  120. [(r * 255).round, (g * 255).round, (b * 255).round]
  121. end
  122. # rubocop:enable Style/MethodParameterName
  123. def lighten_or_darken(color, by)
  124. hue, saturation, light = rgb_to_hsl(color.r, color.g, color.b)
  125. light = begin
  126. if light < 50
  127. [100, light + by].min
  128. else
  129. [0, light - by].max
  130. end
  131. end
  132. ColorDiff::Color::RGB.new(*hsl_to_rgb(hue, saturation, light))
  133. end
  134. def palette_from_histogram(result, quantity)
  135. frequencies = result.scan(/([0-9]+)\:/).flatten.map(&:to_f)
  136. hex_values = result.scan(/\#([0-9A-Fa-f]{6,8})/).flatten
  137. total_frequencies = frequencies.reduce(&:+).to_f
  138. frequencies.map.with_index { |f, i| [f / total_frequencies, hex_values[i]] }
  139. .sort_by { |r| -r[0] }
  140. .reject { |r| r[1].size == 8 && r[1].end_with?('00') }
  141. .map { |r| ColorDiff::Color::RGB.new(*r[1][0..5].scan(/../).map { |c| c.to_i(16) }) }
  142. .slice(0, quantity)
  143. end
  144. def rgb_to_hex(rgb)
  145. '#%02x%02x%02x' % [rgb.r, rgb.g, rgb.b]
  146. end
  147. end
  148. end