A library for rendering element colors in HDR, for Svelte, web components, or plain JS.
| Import | For | Needs Svelte? |
|---|---|---|
@cattn/hdr |
Svelte apps (<HDRRouter>, {@attach hdrify()}) |
yes |
@cattn/hdr/elements |
Any page/framework (<hdr-router>, <hdr-ify>) |
no (runtime bundled) |
@cattn/hdr/core |
Plain JS / other frameworks (applyHdr(el)) |
no |
Use one import per app
npm i @cattn/hdrImport everything from @cattn/hdr.
import { HDRRouter } from '@cattn/hdr';Place in src/+layout.svelte:
<script lang="ts">
import { HDRRouter } from '@cattn/hdr';
</script>
<!-- This should be at the top of your file! -->
<HDRRouter />Amplification
Adjust HDR intensity across platforms (default 1.8).
<HDRRouter amplification={1.5} />Delay
Apply an artificial delay before HDR styles (default 0 ms).
<HDRRouter delay={100} />Converts the element's color to HDR.
<script lang="ts">
import { hdrify } from '@cattn/hdr';
</script>
<h2 style="color: #00ff00" {@attach hdrify()}>The color is in HDR!!</h2>Converts the element's background-color to HDR.
<script lang="ts">
import { hdrifyBackground } from '@cattn/hdr';
</script>
<h2 style="background-color: #00ff00" {@attach hdrifyBackground()}>The background is in HDR!!</h2>Pass a $state rune or a hex string to set HDR color dynamically.
<script lang="ts">
import { hdrifyHex } from '@cattn/hdr';
let color = $state('#ff006f');
</script>
<h2 {@attach hdrifyHex(color)}>The color is in HDR!!</h2>Pass a $state rune or a hex string to set HDR background dynamically.
<script lang="ts">
import { hdrifyBackgroundHex } from '@cattn/hdr';
let color = $state('#ff006f');
</script>
<h2 {@attach hdrifyBackgroundHex(color)}>The background is in HDR!!</h2>Set HDR on or off globally.
<script lang="ts">
import { setHdrEnabled } from '@cattn/hdr';
</script>
<button on:click={() => setHdrEnabled(false)}>Disable HDR</button>
<button on:click={() => setHdrEnabled(true)}>Enable HDR</button>Convenience helper to enable HDR globally.
<script lang="ts">
import { enableHDR } from '@cattn/hdr';
</script>
<button on:click={enableHDR}>Enable HDR</button>Convenience helper to disable HDR globally.
<script lang="ts">
import { disableHDR } from '@cattn/hdr';
</script>
<button on:click={disableHDR}>Disable HDR</button>Read the current HDR state.
<script lang="ts">
import { isHdrEnabled } from '@cattn/hdr';
const hdrOn = isHdrEnabled();
</script>
<p>HDR is {hdrOn ? 'enabled' : 'disabled'}.</p>Set logging on or off globally.
<script lang="ts">
import { setLogEnabled } from '@cattn/hdr';
</script>
<button on:click={() => setLogEnabled(false)}>Disable Logging</button>
<button on:click={() => setLogEnabled(true)}>Enable Logging</button>Convenience helper to enable logging globally.
<script lang="ts">
import { enableLog } from '@cattn/hdr';
</script>
<button on:click={enableLog}>Enable Logging</button>Convenience helper to disable logging globally.
<script lang="ts">
import { disableLog } from '@cattn/hdr';
</script>
<button on:click={disableLog}>Disable Logging</button>Read the current logging state.
<script lang="ts">
import { isLogEnabled } from '@cattn/hdr';
const logOn = isLogEnabled();
</script>
<p>Logging is {logOn ? 'enabled' : 'disabled'}.</p>Import from @cattn/hdr/elements. No Svelte needed.
Import the package once, then place <hdr-router> at the top of <body>.
<script type="module">
import '@cattn/hdr/elements';
</script>
<hdr-router></hdr-router>Amplification and Delay work the same as on HDRRouter.
<hdr-router amplification="1.5" delay="100"></hdr-router>Converts the text color to HDR.
<h2 style="color: #00ff00"><hdr-ify>The color is in HDR!!</hdr-ify></h2>Pass color to set it directly.
<hdr-ify color="#ff006f">The color is in HDR!!</hdr-ify>Converts the background color to HDR. Displays as a block by default.
<hdr-ify background style="background-color: #00ff00">The background is in HDR!!</hdr-ify>
<hdr-ify background color="#ff006f">The background is in HDR!!</hdr-ify>The same HDR and logging helpers as in Svelte are exported here too.
<script type="module">
import { disableHDR } from '@cattn/hdr/elements';
disableHDR();
</script>Import from @cattn/hdr/core. No Svelte needed.
Converts an element's color to HDR. Returns a function that removes it.
import { applyHdr } from '@cattn/hdr/core';
const stop = applyHdr(element);
const stopBg = applyHdr(element, { mode: 'background', color: '#ff006f' });The same HDR and logging helpers as in Svelte are exported here too.
import { disableHDR } from '@cattn/hdr/core';
disableHDR();Any CSS color works: hex, rgb(), hsl(), oklch(), named colors, and more.
Text colors update automatically, including :hover and inherited colors. Background colors from a class or stylesheet are only read once, so pass the color directly if it changes.
After npm install, start a development server:
npm run devLibrary code lives in src/lib, web components in src/elements, and demos in src/routes (/elements for web components).
To build the library:
npm run build