Skip to content

Commit

Permalink
fix: color in hsl with unit like deg,turn should render correctly i…
Browse files Browse the repository at this point in the history
…n `@resvg/resvg-js` (#449)

Close #374


Releated issue: RazrFalcon/resvg#579
  • Loading branch information
Jackie1210 committed Apr 18, 2023
1 parent 44f4a6b commit 4405d09
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"css-to-react-native": "^3.0.0",
"emoji-regex": "^10.2.1",
"linebreak": "^1.1.0",
"parse-css-color": "^0.2.1",
"postcss-value-parser": "^4.2.0",
"yoga-wasm-web": "^0.3.3"
},
Expand Down
15 changes: 14 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions src/handler/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { getPropertyName, getStylesForProperty } from 'css-to-react-native'
import { parseElementStyle } from 'css-background-parser'
import { parse as parseBoxShadow } from 'css-box-shadow'
import cssColorParse from 'parse-css-color'

import CssDimension from '../vendor/parse-css-dimension/index.js'
import parseTransformOrigin from '../transform-origin.js'
Expand Down Expand Up @@ -369,12 +370,28 @@ export default function expand(
return transformedStyle
}

/**
* @see https://github.com/RazrFalcon/resvg/issues/579
*/
function refineHSL(color: string) {
if (color.startsWith('hsl')) {
const t = cssColorParse(color)
const [h, s, l] = t.values

return `hsl(${[h, `${s}%`, `${l}%`]
.concat(t.alpha === 1 ? [] : [t.alpha])
.join(',')})`
}

return color
}

function getCurrentColor(color: string | undefined, inheritedColor: string) {
if (color && color.toLowerCase() !== 'currentcolor') {
return color
return refineHSL(color)
}

return inheritedColor
return refineHSL(inheritedColor)
}

function convertCurrentColorToActualValue(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions test/color-models.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,61 @@ describe('Color Models', () => {
})
})

it('should support css4 synatx color in hsl', async () => {
const svg = await satori(
<div
style={{
fontSize: 16,
background: 'white',
width: '100%',
height: '100%',
display: 'flex',
textAlign: 'center',
alignItems: 'center',
justifyContent: 'center',
}}
>
<span style={{ color: 'hsl(200deg, 50%, 50%)' }}>A</span>
<span style={{ color: 'hsl(200deg, 50%, 50%, 0.6)' }}>A</span>
<span style={{ color: 'hsl(200, 50%, 50%)' }}>B</span>
<span style={{ color: 'hsl(0.3turn, 50%, 50%)' }}>C</span>
<span style={{ color: 'hsl(0.3turn, 50%, 50%, 0.6)' }}>D</span>
</div>,
{
width: 100,
height: 100,
fonts,
}
)
expect(toImage(svg, 100)).toMatchImageSnapshot()
})

it('should support css4 synatx color in hsl if inherited', async () => {
const svg = await satori(
<div
style={{
fontSize: 16,
background: 'white',
width: '100%',
height: '100%',
display: 'flex',
textAlign: 'center',
alignItems: 'center',
justifyContent: 'center',
color: 'hsl(200deg, 50%, 50%)',
}}
>
<span>A</span>
</div>,
{
width: 100,
height: 100,
fonts,
}
)
expect(toImage(svg, 100)).toMatchImageSnapshot()
})

// Borders: shorthand, border-bottom-color, border-color, border-left-color, border-right-color, border-top-color

// Box shadow
Expand Down

1 comment on commit 4405d09

@vercel
Copy link

@vercel vercel bot commented on 4405d09 Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.