Skip to content

Commit

Permalink
fixes readability values and adds power value
Browse files Browse the repository at this point in the history
  • Loading branch information
CarelessCourage committed Jan 9, 2024
1 parent b2ba2b7 commit f87b6a8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
3 changes: 2 additions & 1 deletion packages/core/engine/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { UmbraInput, UmbraSettings } from './types'

export const defaultSettings: UmbraSettings = {
readability: 10,
power: 0.15,
iterations: 15,
readability: 70,
insertion: 9,
shades: [5, 5, 5, 5, 15, 10, 10, 25, 30, 25, 25, 25],
tints: [5, 10, 10, 10, 15, 15, 25, 15, 15, 15, 15, 25]
Expand Down
2 changes: 2 additions & 0 deletions packages/core/engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ function umbraAdjust(scheme = defaultScheme) {
const background = colord(scheme.background)
const foreground = getReadable({
readability: scheme.settings?.readability || 4,
iterations: scheme.settings?.iterations || 15,
power: scheme.settings?.power || 15,
foreground: colord(scheme.foreground),
background
})
Expand Down
21 changes: 16 additions & 5 deletions packages/core/engine/primitives/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ type ColorRawRange = {
background: string | Colord
readability?: number
iterations?: number
power?: number
}

interface IncreaseContrastUntil {
color: Colord
contrast?: Colord
iterations?: number
power?: number
condition: (newColor: Colord, iterations?: number) => boolean
}

Expand All @@ -28,7 +30,8 @@ interface MoveAwayFrom {

const stored = {
readability: defaultSettings.readability || 11,
iterations: defaultSettings.iterations || 15
iterations: defaultSettings.iterations || 15,
power: defaultSettings.power || 15
}

function apcaContrast(fg: string | Colord, bg: string | Colord) {
Expand All @@ -41,15 +44,22 @@ export const getReadability = (fg: string | Colord, bg: string | Colord) => {
return apcaContrast(fg, bg)
}

export const getReadable = ({ foreground, background, readability, iterations }: ColorRawRange) => {
export const getReadable = ({
foreground,
background,
readability,
iterations,
power
}: ColorRawRange) => {
const color = colord(foreground)
const contrast = colord(background)
return increaseContrastUntil({
color,
contrast,
iterations: iterations || stored.iterations,
power: power || stored.power,
condition: (c) => {
const current = Math.abs(getReadability(c, background))
const current = Math.abs(getReadability(c, contrast))
return current > (readability || stored.readability)
}
})
Expand All @@ -59,14 +69,15 @@ export function increaseContrastUntil({
color,
contrast,
condition,
iterations = 15
iterations = 15,
power = 15
}: IncreaseContrastUntil) {
let newColor = color
let count = 0
while (!condition(newColor, count) && count < iterations) {
count += 1
newColor = increaseContrast({
val: iterations,
val: power,
color: newColor,
contrast
})
Expand Down
3 changes: 2 additions & 1 deletion packages/core/engine/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ export interface UmbraAdjusted {
}

export interface UmbraSettings {
readability?: number
power?: number
iterations?: number
readability?: number
insertion?: number
aliases?: Alias | true
shades?: (number | string)[]
Expand Down
18 changes: 5 additions & 13 deletions packages/playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,11 @@ const accent3 = {
color: '#e5484d'
}
const theme = umbra(
{
background: '#ffffff',
foreground: '#ffffff',
accents: [royal, accent, radixRed, radixYellow, radixBlue, accent3, radixRed, success, brown],
settings: {
readability: 2
}
}
// {
// readability: 90
// }
).apply({ alias: true })
const theme = umbra({
background: '#000000',
foreground: '#ffffff',
accents: [royal, accent, radixRed, radixYellow, radixBlue, accent3, radixRed, success, brown]
}).apply({})
// const theme = umbra({
// foreground: '#ffffff',
Expand Down

0 comments on commit f87b6a8

Please sign in to comment.