rename project from SimpleshopTheme to Berrypod
All modules, configs, paths, and references updated. 836 tests pass, zero warnings. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
68
lib/berrypod/media/image.ex
Normal file
68
lib/berrypod/media/image.ex
Normal file
@@ -0,0 +1,68 @@
|
||||
defmodule Berrypod.Media.Image do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
@foreign_key_type :binary_id
|
||||
|
||||
schema "images" do
|
||||
field :image_type, :string
|
||||
field :filename, :string
|
||||
field :content_type, :string
|
||||
field :file_size, :integer
|
||||
field :data, :binary
|
||||
field :is_svg, :boolean, default: false
|
||||
field :svg_content, :string
|
||||
field :source_width, :integer
|
||||
field :source_height, :integer
|
||||
field :variants_status, :string, default: "pending"
|
||||
|
||||
timestamps(type: :utc_datetime)
|
||||
end
|
||||
|
||||
@max_file_size 5_000_000
|
||||
|
||||
@doc false
|
||||
def changeset(image, attrs) do
|
||||
image
|
||||
|> cast(attrs, [
|
||||
:image_type,
|
||||
:filename,
|
||||
:content_type,
|
||||
:file_size,
|
||||
:data,
|
||||
:is_svg,
|
||||
:svg_content,
|
||||
:source_width,
|
||||
:source_height,
|
||||
:variants_status
|
||||
])
|
||||
|> validate_required([:image_type, :filename, :content_type, :file_size, :data])
|
||||
|> validate_inclusion(:image_type, ~w(logo header product))
|
||||
|> validate_number(:file_size, less_than: @max_file_size)
|
||||
|> detect_svg()
|
||||
end
|
||||
|
||||
defp detect_svg(changeset) do
|
||||
content_type = get_change(changeset, :content_type)
|
||||
|
||||
if content_type == "image/svg+xml" or
|
||||
String.ends_with?(get_change(changeset, :filename) || "", ".svg") do
|
||||
changeset
|
||||
|> put_change(:is_svg, true)
|
||||
|> maybe_store_svg_content()
|
||||
else
|
||||
changeset
|
||||
end
|
||||
end
|
||||
|
||||
defp maybe_store_svg_content(changeset) do
|
||||
case get_change(changeset, :data) do
|
||||
nil ->
|
||||
changeset
|
||||
|
||||
svg_binary when is_binary(svg_binary) ->
|
||||
put_change(changeset, :svg_content, svg_binary)
|
||||
end
|
||||
end
|
||||
end
|
||||
135
lib/berrypod/media/svg_recolorer.ex
Normal file
135
lib/berrypod/media/svg_recolorer.ex
Normal file
@@ -0,0 +1,135 @@
|
||||
defmodule Berrypod.Media.SVGRecolorer do
|
||||
@moduledoc """
|
||||
Recolors SVG images by replacing fill and stroke colors with a target color.
|
||||
|
||||
This module provides functionality to dynamically recolor SVG logos
|
||||
to match the site's branding colors.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Recolors an SVG by replacing common color attributes with the target color.
|
||||
|
||||
Replaces:
|
||||
- fill="..." attributes (except fill="none")
|
||||
- stroke="..." attributes (except stroke="none")
|
||||
- style="fill:..." inline styles
|
||||
- style="stroke:..." inline styles
|
||||
|
||||
## Examples
|
||||
|
||||
iex> svg = ~s(<svg><path fill="#000000" d="..."/></svg>)
|
||||
iex> SVGRecolorer.recolor(svg, "#ff6600")
|
||||
~s(<svg><path fill="#ff6600" d="..."/></svg>)
|
||||
|
||||
"""
|
||||
@spec recolor(String.t(), String.t()) :: String.t()
|
||||
def recolor(svg_content, target_color)
|
||||
when is_binary(svg_content) and is_binary(target_color) do
|
||||
svg_content
|
||||
|> recolor_fill_attributes(target_color)
|
||||
|> recolor_stroke_attributes(target_color)
|
||||
|> recolor_inline_fill_styles(target_color)
|
||||
|> recolor_inline_stroke_styles(target_color)
|
||||
|> recolor_css_fill_styles(target_color)
|
||||
|> recolor_css_stroke_styles(target_color)
|
||||
|> recolor_current_color(target_color)
|
||||
end
|
||||
|
||||
defp recolor_fill_attributes(svg, color) do
|
||||
Regex.replace(
|
||||
~r/fill\s*=\s*["'](?!none)([^"']+)["']/i,
|
||||
svg,
|
||||
~s(fill="#{color}")
|
||||
)
|
||||
end
|
||||
|
||||
defp recolor_stroke_attributes(svg, color) do
|
||||
Regex.replace(
|
||||
~r/stroke\s*=\s*["'](?!none)([^"']+)["']/i,
|
||||
svg,
|
||||
~s(stroke="#{color}")
|
||||
)
|
||||
end
|
||||
|
||||
defp recolor_inline_fill_styles(svg, color) do
|
||||
Regex.replace(
|
||||
~r/style\s*=\s*["']([^"']*)fill\s*:\s*(?!none)[^;}"']+([^"']*)["']/i,
|
||||
svg,
|
||||
~s(style="\\1fill:#{color}\\2")
|
||||
)
|
||||
end
|
||||
|
||||
defp recolor_inline_stroke_styles(svg, color) do
|
||||
Regex.replace(
|
||||
~r/style\s*=\s*["']([^"']*)stroke\s*:\s*(?!none)[^;}"']+([^"']*)["']/i,
|
||||
svg,
|
||||
~s(style="\\1stroke:#{color}\\2")
|
||||
)
|
||||
end
|
||||
|
||||
defp recolor_css_fill_styles(svg, color) do
|
||||
# Replace fill declarations in CSS style blocks: fill:#XXXXXX or fill: #XXXXXX
|
||||
# But preserve fill:none
|
||||
Regex.replace(
|
||||
~r/fill\s*:\s*(?!none)(#[0-9A-Fa-f]{3,6}|[a-zA-Z]+)(?=[;\s\}])/,
|
||||
svg,
|
||||
"fill:#{color}"
|
||||
)
|
||||
end
|
||||
|
||||
defp recolor_css_stroke_styles(svg, color) do
|
||||
# Replace stroke declarations in CSS style blocks: stroke:#XXXXXX or stroke: #XXXXXX
|
||||
# But preserve stroke:none
|
||||
Regex.replace(
|
||||
~r/stroke\s*:\s*(?!none)(#[0-9A-Fa-f]{3,6}|[a-zA-Z]+)(?=[;\s\}])/,
|
||||
svg,
|
||||
"stroke:#{color}"
|
||||
)
|
||||
end
|
||||
|
||||
defp recolor_current_color(svg, color) do
|
||||
String.replace(svg, "currentColor", color)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Validates that a string is a valid hex color code.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> SVGRecolorer.valid_hex_color?("#ff6600")
|
||||
true
|
||||
|
||||
iex> SVGRecolorer.valid_hex_color?("#f60")
|
||||
true
|
||||
|
||||
iex> SVGRecolorer.valid_hex_color?("invalid")
|
||||
false
|
||||
|
||||
"""
|
||||
@spec valid_hex_color?(String.t()) :: boolean()
|
||||
def valid_hex_color?(color) when is_binary(color) do
|
||||
Regex.match?(~r/^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/, color)
|
||||
end
|
||||
|
||||
def valid_hex_color?(_), do: false
|
||||
|
||||
@doc """
|
||||
Normalizes a hex color to 6-digit format.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> SVGRecolorer.normalize_hex_color("#f60")
|
||||
"#ff6600"
|
||||
|
||||
iex> SVGRecolorer.normalize_hex_color("#ff6600")
|
||||
"#ff6600"
|
||||
|
||||
"""
|
||||
@spec normalize_hex_color(String.t()) :: String.t()
|
||||
def normalize_hex_color("#" <> hex) when byte_size(hex) == 3 do
|
||||
[r, g, b] = String.graphemes(hex)
|
||||
"##{r}#{r}#{g}#{g}#{b}#{b}"
|
||||
end
|
||||
|
||||
def normalize_hex_color(color), do: color
|
||||
end
|
||||
Reference in New Issue
Block a user