polished#parseToRgb TypeScript Examples

The following examples show how to use polished#parseToRgb. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Colors.stories.tsx    From kodiak-ui with MIT License 6 votes vote down vote up
function flattenThemeColors({ theme }: { theme: Theme }) {
  const flattenedThemeColors: Array<{
    colorKey: string
    color: string
    rgbColor: RgbColor | RgbaColor
  }> = []

  deepMapKeys({
    obj: theme.colors,
    visitorfunction: ({ value, key, parentKeys }) => {
      const colorKey =
        parentKeys !== undefined ? [parentKeys, key].join('.') : key

      if (!colorKey) {
        return
      }

      try {
        const rgbColor = parseToRgb(value as string)

        flattenedThemeColors.push({
          colorKey: colorKey,
          color: value,
          rgbColor: rgbColor,
        })
      } catch (error) {
        console.error(`Couldn't parse ${colorKey}:${value}`)
        console.error(error)
      }
    },
  })

  return flattenedThemeColors
}
Example #2
Source File: Colors.stories.tsx    From kodiak-ui with MIT License 6 votes vote down vote up
function findNearestThemeColors({
  theme,
  color,
  count,
}: {
  theme: Theme
  color: string
  count: number
}) {
  try {
    const { red, green, blue } = parseToRgb(color)

    const flattenedThemeColors = flattenThemeColors({ theme })
    const colorsWithDistance = flattenedThemeColors
      .map(color => ({
        ...color,
        distance: Math.sqrt(
          2 * Math.pow(color.rgbColor.red - red, 2) +
            4 * Math.pow(color.rgbColor.green - green, 2) +
            3 * Math.pow(color.rgbColor.blue - blue, 2),
        ),
      }))
      .sort((a, b) => a.distance - b.distance)

    colorsWithDistance.length = count

    return colorsWithDistance
  } catch (e) {
    // couldn't parse the color
    return null
  }
}