Skip to content

Instantly share code, notes, and snippets.

@PaulRBerg
Created November 17, 2023 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulRBerg/b4a9706b43734d73c8c6c5a8850ce611 to your computer and use it in GitHub Desktop.
Save PaulRBerg/b4a9706b43734d73c8c6c5a8850ce611 to your computer and use it in GitHub Desktop.
Function that generates a pseudo-random HSL color
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.8.19;
/// @notice Generates a pseudo-random HSL color by hashing together the `chainid`, the `sablier` address,
/// and the `streamId`. This will be used as the accent color for the SVG.
function generateAccentColor(address sablier, uint256 streamId) internal view returns (string memory) {
uint256 chainId = block.chainid;
uint32 bitField = uint32(uint256(keccak256(abi.encodePacked(chainId, sablier, streamId))));
unchecked {
uint256 hue = (bitField >> 16) % 360;
uint256 saturation = ((bitField >> 8) & 0xFF) % 80 + 20;
uint256 lightness = (bitField & 0xFF) % 70 + 30;
return string.concat("hsl(", hue.toString(), ",", saturation.toString(), "%,", lightness.toString(), "%)");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment