Skip to content

Commit

Permalink
chore: body and head refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jpgonzalezra committed Jan 21, 2024
1 parent 06aeec4 commit eba8ee5
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 139 deletions.
62 changes: 56 additions & 6 deletions packages/contracts/src/LucidOrigins.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,20 @@ contract LucidOrigins is Owned, ERC721A, Background, Face, Body, Head, Blush {
linesColor
);

(string memory head, string memory head2) = head(
(string memory colorDefs, string memory fillColor) = getColor(r, g, b, r2, g2, b2, dna[Constants.BASE_INDEX]);
(string memory head, string memory stroke) = head(
normalizeToRange(dna[Constants.HEAD_SIZE_INDEX], 85, 88),
normalizeToRange(dna[Constants.HEAD_SIZE_INDEX], 0, 2),
normalizeToRange(dna[Constants.HEAD_MIN_GROWTH_INDEX], 6, 9),
normalizeToRange(dna[Constants.HEAD_EDGES_NUM_INDEX], 7, 9)
normalizeToRange(dna[Constants.HEAD_EDGES_NUM_INDEX], 7, 20),
colorDefs,
fillColor
);

(string memory body, string memory stroke, string memory baseBody) =
body(r, g, b, r2, g2, b2, head, head2, normalizeToRange(dna[Constants.HEAD_SIZE_INDEX], 0, 50));

string memory footer = "</svg>";
string memory svg = string(abi.encodePacked(header, background, baseBody, body, stroke, blush(), face, footer));
string memory svg = string(
abi.encodePacked(header, background, body(colorDefs, fillColor), head, stroke, blush(), face, footer)
);

return metadata(name, svg);
}
Expand Down Expand Up @@ -115,4 +117,52 @@ contract LucidOrigins is Owned, ERC721A, Background, Face, Body, Head, Blush {
function isColorDark(uint256 r, uint256 g, uint256 b) public pure returns (bool) {
return (2126 * r + 7152 * g + 722 * b) / 10_000 < 128;
}

function getColor(
uint256 r,
uint256 g,
uint256 b,
uint256 r2,
uint256 g2,
uint256 b2,
uint256 base
)
internal
pure
returns (string memory, string memory)
{
bool isPlain = base < 20;
string memory colorDefs = isPlain
? ""
: string(
abi.encodePacked(
"<defs>",
'<linearGradient id="linear-grad">',
'<stop offset="0" stop-color="',
"rgb(",
r.toString(),
",",
g.toString(),
",",
b.toString(),
')"/>',
'<stop offset="1" stop-color="',
"rgb(",
r2.toString(),
",",
g2.toString(),
",",
b2.toString(),
')"/>',
"</linearGradient>",
"</defs>"
)
);

string memory fillColor = isPlain
? string(abi.encodePacked("rgb(", r.toString(), ",", g.toString(), ",", b.toString(), ")"))
: "url(#linear-grad)";

return (colorDefs, fillColor);
}
}
132 changes: 1 addition & 131 deletions packages/contracts/src/layers/Body.sol
Original file line number Diff line number Diff line change
@@ -1,138 +1,8 @@
// SPDX-License-Identifier: GNU GPLv3
pragma solidity 0.8.21;

// import { console2 } from "forge-std/console2.sol";
import { LibString } from "solmate/utils/LibString.sol";

contract Body {
using LibString for uint256;

function body(
uint256 r,
uint256 g,
uint256 b,
uint256 r2,
uint256 g2,
uint256 b2,
string memory head,
string memory head2,
uint256 base
)
internal
pure
returns (string memory, string memory, string memory)
{
(string memory colorDefs, string memory fillColor) = getColor(r, g, b, r2, g2, b2, base);
return (getBody(colorDefs, fillColor, head, head2), getStroke(head, head2), getBaseBody(colorDefs, fillColor));
}

function getColor(
uint256 r,
uint256 g,
uint256 b,
uint256 r2,
uint256 g2,
uint256 b2,
uint256 base
)
internal
pure
returns (string memory, string memory)
{
bool isPlain = base < 20;
string memory colorDefs = isPlain
? ""
: string(
abi.encodePacked(
"<defs>",
'<linearGradient id="linear-grad">',
'<stop offset="0" stop-color="',
"rgb(",
r.toString(),
",",
g.toString(),
",",
b.toString(),
')"/>',
'<stop offset="1" stop-color="',
"rgb(",
r2.toString(),
",",
g2.toString(),
",",
b2.toString(),
')"/>',
"</linearGradient>",
"</defs>"
)
);

string memory fillColor = isPlain
? string(abi.encodePacked("rgb(", r.toString(), ",", g.toString(), ",", b.toString(), ")"))
: "url(#linear-grad)";

return (colorDefs, fillColor);
}

function getBody(
string memory colorDefs,
string memory fillColor,
string memory head,
string memory head2
)
internal
pure
returns (string memory)
{
return string(
abi.encodePacked(
colorDefs,
'<path id="head" d="',
head,
'Z" transform-origin="center" fill="',
fillColor,
'">',
'<animate attributeName="d" values="',
head,
";",
head2,
";",
head,
'" dur="15s" id="body-anim" repeatCount="indefinite"',
' keysplines=".42 0 1 1; 0 0 .59 1; .42 0 1 1; 0 0 .59 1;',
' .42 0 1 1; 0 0 .59 1; .42 0 1 1; 0 0 .59 1;"/>',
'<animateTransform attributeName="transform" type="rotate" ',
'from="0" to="10" dur="1000" repeatCount="indefinite" />',
"</path>"
)
);
}

function getStroke(string memory head, string memory head2) internal pure returns (string memory) {
return string(
abi.encodePacked(
'<path d="',
head,
'Z" id="body-stroke" stroke="',
"black",
'" stroke-width="2" fill="none" transform-origin="center">',
'<animate attributeName="d" values="',
head,
";",
head2,
";",
head,
'" dur="16s" id="stroke-anim" repeatCount="indefinite" ',
'begin="body-anim.begin + 2s" keysplines=".42 0 1 1; 0 0 .59 1; ',
'.42 0 1 1; 0 0 .59 1; .42 0 1 1; 0 0 .59 1; .42 0 1 1; 0 0 .59 1;"/>',
'<animateTransform attributeName="transform" type="rotate" from="0" ',
'to="30" dur="1000" repeatCount="indefinite" />',
"</path>"
)
);
}

function getBaseBody(string memory colorDefs, string memory fillColor) internal pure returns (string memory) {
function body(string memory colorDefs, string memory fillColor) internal pure returns (string memory) {
return string(
abi.encodePacked(
colorDefs,
Expand Down
66 changes: 64 additions & 2 deletions packages/contracts/src/layers/Head.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ contract Head {
uint256 size,
uint256 animation,
uint256 minGrowth,
uint256 edgesNum
uint256 edgesNum,
string memory colorDefs,
string memory fillColor
)
internal
view
returns (string memory, string memory)
{
Point[] memory points = createPoints(size, minGrowth, edgesNum);
return (createSvgPath(points), createSvgPath(createPoints(size + animation, minGrowth, edgesNum)));
string memory h1 = createSvgPath(points);
string memory h2 = createSvgPath(createPoints(size + animation, minGrowth, edgesNum));
return (buildHead(colorDefs, fillColor, h1, h2), buildStroke(h1, h2));
}

function createPoints(uint256 size, uint256 minGrowth, uint256 edgesNum) internal view returns (Point[] memory) {
Expand Down Expand Up @@ -92,4 +96,62 @@ contract Head {

return string(abi.encodePacked(svgPath, "Z"));
}

function buildHead(
string memory colorDefs,
string memory fillColor,
string memory h1,
string memory h2
)
private
pure
returns (string memory)
{
return string(
abi.encodePacked(
colorDefs,
'<path id="head" d="',
h1,
'Z" transform-origin="center" fill="',
fillColor,
'">',
'<animate attributeName="d" values="',
h1,
";",
h2,
";",
h1,
'" dur="15s" id="body-anim" repeatCount="indefinite"',
' keysplines=".42 0 1 1; 0 0 .59 1; .42 0 1 1; 0 0 .59 1;',
' .42 0 1 1; 0 0 .59 1; .42 0 1 1; 0 0 .59 1;"/>',
'<animateTransform attributeName="transform" type="rotate" ',
'from="0" to="10" dur="1000" repeatCount="indefinite" />',
"</path>"
)
);
}

function buildStroke(string memory h, string memory h2) private pure returns (string memory) {
return string(
abi.encodePacked(
'<path d="',
h,
'Z" id="body-stroke" stroke="',
"black",
'" stroke-width="2" fill="none" transform-origin="center">',
'<animate attributeName="d" values="',
h,
";",
h2,
";",
h,
'" dur="16s" id="stroke-anim" repeatCount="indefinite" ',
'begin="body-anim.begin + 2s" keysplines=".42 0 1 1; 0 0 .59 1; ',
'.42 0 1 1; 0 0 .59 1; .42 0 1 1; 0 0 .59 1; .42 0 1 1; 0 0 .59 1;"/>',
'<animateTransform attributeName="transform" type="rotate" from="0" ',
'to="30" dur="1000" repeatCount="indefinite" />',
"</path>"
)
);
}
}
2 changes: 2 additions & 0 deletions packages/contracts/src/utils/constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ library Constants {
uint16 public constant R2_INDEX = 13;
uint16 public constant G2_INDEX = 14;
uint16 public constant B2_INDEX = 15;

uint16 public constant BASE_INDEX = 16;
}

0 comments on commit eba8ee5

Please sign in to comment.