XYHASH
Overview
The XYXYX smart contract has a built-in hash function called XYHASH, which provides uniqueness to each token output.
Unlike traditional hash functions that output a string of numbers and characters, (e.g., SHA256) XYHASH generates outputs in the form of Hex color codes, utilizing the tokenId
of each XYXYX as the input variable.
For each combination of numbers that represents a tokenId
— which is always unique for each token —, a unique combination of two Hex colors is produced as hashed output by the XYXYX smart contract.
Code
Code Explained
getColorComponentRed, getColorComponentGreen, getColorComponentBlue: These three functions extract the red, green, and blue components of a color from a
uint256
value. This is achieved by bit-shifting the value to the right (>>
) and applying a bitwise, and operation (&
) with0xf
(hexadecimal for 15) to isolate the last four bits. The bit-shifting distances (8 for red, 4 for green, and 0 for blue) position the bits of interest in the rightmost position to apply the mask (0xf
). The result is cast touint16
and represents the color component value in the range of 0 to 15.Red Component: Extracted by shifting the value 8 bits to the right and isolating the last four bits.
Green Component: Extracted by shifting the value 4 bits to the right and isolating the last four bits.
Blue Component: Extracted by directly isolating the last four bits of the value.
_HEX_SYMBOLS: A private constant byte array containing the hexadecimal symbols '0' to 'f'. This is used to map the numeric color component values (0-15) to their hexadecimal character representations.
getColorHexCode: This function uses the three color component functions to obtain the red, green, and blue values of a color, maps these values to their hexadecimal characters using the
_HEX_SYMBOLS
array, and constructs a hexadecimal color code string. The color code string starts with a "#" followed by two characters for each color component, doubling each character for simplicity (e.g., if the red component is0xf
, it will be represented as "ff" in the string).xyHash: A function that generates a hash value based on a given string prefix and a
uint256
tokenId
. It uses thekeccak256
hash function on the concatenated string andtokenId
, then takes the result modulo 4096 (refers to the mathematical operation known as the "modulo" operation, which finds the remainder when one number is divided by another. In this context, after computing the hash function (likely producing a large integer as a result), the modulo operation is applied with 4096 as the divisor.). This ensures a fixed range of possible outcomes, which is useful for generating predictable yet unique values for color determination.getColor1 and getColor2: These public functions generate color values by calling
xyHash
with different string prefixes ("1" and "2", respectively), and the giventokenId
. The resultant hash values can then be used to determine unique colors for each, potentially for use in generating or coloring tokens.
Last updated