Created
November 17, 2023 19:52
-
-
Save PaulRBerg/b4a9706b43734d73c8c6c5a8850ce611 to your computer and use it in GitHub Desktop.
Function that generates a pseudo-random HSL color
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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