Skip to content

Commit

Permalink
fix: type fix (#26)
Browse files Browse the repository at this point in the history
* fix: type fix

* fix: type fix

* chore: remove empty line

* chore: export adjustment
  • Loading branch information
zhangmo8 authored Jul 10, 2024
1 parent 5a14c73 commit 7a66689
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
38 changes: 32 additions & 6 deletions packages/magic-color/src/core/basic.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { ColorType, Colors, HexColor, HsbColor, HslColor, LabColor, Opacity, RgbColor } from '@magic-color/core'
import { hexToHsb, hexToHsl, hexToLab, hexToRgb, hsbToHex, hsbToHsl, hsbToLab, hsbToRgb, hslToHex, hslToHsb, hslToLab, hslToRgb, labToHex, labToHsb, labToHsl, labToRgb, rgbToHex, rgbToHsb, rgbToHsl, rgbToLab } from '@magic-color/core'
import { getColorName } from '../theme'

import { getColorName, theme } from '../theme'
import { hash } from '../hash'
import { calcAPCA, calcWCAG, getReadableTextColor, isWarmColor, reverseAPCA } from '../contrast'
import { random } from '../utils'
import { SupportTypes, alphaToString, resolveArgs, valid } from './utils'
import type { ColorObject } from './types'
import { SupportTypes, alphaToString, resolveArgs } from './utils'

export class Magicolor<T extends ColorType> implements ColorObject<T> {
type: T
Expand Down Expand Up @@ -303,9 +305,33 @@ export class Magicolor<T extends ColorType> implements ColorObject<T> {
}
}

export function mc<T extends ColorType>(value: Colors[T] | Record<string, number>, type?: T, alpha?: Opacity): Magicolor<T>
export function mc<T extends ColorType = 'rgb'>(v1: number, v2: number, v3: number, type?: T, alpha?: Opacity): Magicolor<T>
export function mc<T extends ColorType>(...args: any): Magicolor<T> {
export interface MagicolorInstance {
<T extends ColorType>(value: Colors[T] | Record<string, number>, type?: T, alpha?: Opacity): Magicolor<T>
<T extends ColorType = 'rgb'>(v1: number, v2: number, v3: number, type?: T, alpha?: Opacity): Magicolor<T>
valid: typeof valid
random: typeof random
hash: typeof hash
theme: typeof theme
getColorName: typeof getColorName
wcag: typeof calcWCAG
apca: typeof calcAPCA
apcaReverse: typeof reverseAPCA
readable: typeof getReadableTextColor
warm: typeof isWarmColor
}

export const mc: MagicolorInstance = <T extends ColorType>(...args: any[]): Magicolor<T> => {
// @ts-expect-error allow the type to be inferred
return new Magicolor(...args) as Magicolor<T>
}

mc.valid = valid
mc.random = random
mc.hash = hash
mc.theme = theme
mc.getColorName = getColorName
mc.wcag = calcWCAG
mc.apca = calcAPCA
mc.apcaReverse = reverseAPCA
mc.readable = getReadableTextColor
mc.warm = isWarmColor
34 changes: 1 addition & 33 deletions packages/magic-color/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,2 @@
import { hash } from '../hash'
import { calcAPCA, calcWCAG, getReadableTextColor, isWarmColor, reverseAPCA } from '../contrast'
import { getColorName, theme } from '../theme'
import { random } from '../utils'
import { mc } from './basic'
import { guessType } from './utils'

export { Magicolor } from './basic'
export { Magicolor, mc } from './basic'
export * from './types'

// @ts-expect-error - anyway
mc.valid = (color: string) => guessType(color) !== undefined
// @ts-expect-error - anyway
mc.random = random
// @ts-expect-error - anyway
mc.hash = hash
// @ts-expect-error - anyway
mc.theme = theme
// @ts-expect-error - anyway
mc.getColorName = getColorName
// @ts-expect-error - anyway
mc.wcag = calcWCAG
// @ts-expect-error - anyway
mc.apca = calcAPCA
// @ts-expect-error - anyway
mc.apcaReverse = reverseAPCA
// @ts-expect-error - anyway
mc.readable = getReadableTextColor
// @ts-expect-error - anyway
mc.warm = isWarmColor

export {
mc,
}
4 changes: 4 additions & 0 deletions packages/magic-color/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,7 @@ export function resolveArgs<T extends ColorType = 'rgb'>(...args: any[]): [Color
return [_values, _type, _alpha]
}
}

export function valid(color: string) {
return guessType(color) !== undefined
}
2 changes: 1 addition & 1 deletion packages/magic-color/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from '@magic-color/core'

export * from './core'
export * from './theme/types'
export * from './theme'
export * from './utils'
export * from './hash'
export * from './contrast'
2 changes: 2 additions & 0 deletions packages/magic-color/src/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,5 @@ export function theme(color: string, options: ThemeOptions = {}): ThemeMetas {

return Object.fromEntries(shades) as unknown as ThemeMetas
}

export * from './types'

0 comments on commit 7a66689

Please sign in to comment.