New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@tgrx/getcontrasttext

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tgrx/getcontrasttext

MUI's getContrastText and helpers | TGRstack

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
166
increased by58.1%
Maintainers
1
Weekly downloads
 
Created
Source

Material-UI's getContrastText() function and helpers| TGRstack

Known Vulnerabilities

Development and Production Ready |⸰| VSCode Extensions

TypeScript TSLint TS-Jest

WebPack Node

NPS friendly Commitizen friendly Semver friendly

Install

$ npm i -S @tgrx/getcontrasttext

About

Sometimes its useful to access some of the under the hood functionality of Material-UI. I basically copy pasted some of the functions from MUI-4.0.0-alpha.0 and put them on a /TGRstack/typescript-module. Easy Peasy.

Example

// TYPINGS
import { PaletteType } from '@material-ui/core'
import { SpacingOptions } from '@material-ui/core/styles/spacing'

// COLORS
import indigo from '@material-ui/core/colors/indigo'
import red from '@material-ui/core/colors/red'
import teal from '@material-ui/core/colors/teal'

// HELPERS
import getContrastText from '@tgrx/getcontrasttext'
import { isProd } from '_config/index'

// SETTINGS

// tslint:disable object-literal-sort-keys
const palette = {
  type: ('dark' as PaletteType),
  primary: indigo,
  secondary: teal,
  error: red,
  background: '#000',
  surface: '#111'
}
// tslint:enable object-literal-sort-keys

// Used by `getContrastText()` to maximize the contrast
//    between the background and the text.
const contrastThreshold = 3

// Used to shift a color's luminance by approximately
//   two indexes within its tonal palette.
//   E.g., shift from Red 500 to Red 300 or Red 700.
const tonalOffset = 0.2

const spacing = (4 as SpacingOptions)

const warnings = !isProd

// tslint:disable object-literal-sort-keys
export default {
  palette: {
    type: palette.type,
    primary: {
      light: palette.primary[300],
      main: palette.primary[500],
      dark: palette.primary[700],
      contrastText: getContrastText({  // example of import getContrastText()
        background: palette.primary[500],
        light: {text: {primary: palette.primary[300]}},
        dark: {text: {primary: palette.primary[700]}},
        contrastThreshold,
        warnings,
      }),
    },
    secondary: {
      light: palette.secondary.A200,
      main: palette.secondary.A400,
      dark: palette.secondary.A700,
      contrastText: getContrastText({  // example of import getContrastText()
        background: palette.secondary[500],
        light: {text: {primary: palette.secondary[300]}},
        dark: {text: {primary: palette.secondary[700]}},
        contrastThreshold,
        warnings,
      }),
    },
    error: {
      light: palette.error[300],
      main: palette.error[500],
      dark: palette.error[700],
      contrastText: getContrastText({  // example of import getContrastText()
        background: palette.error[500],
        light: {text: {primary: palette.error[300]}},
        dark: {text: {primary: palette.error[700]}},
        contrastThreshold,
        warnings,
      }),
    },
    background: {
      default: palette.background, // inDesign: surface-dark
      paper:   palette.surface,    // inDesign: surface-main
    },
    contrastThreshold,
    tonalOffset,
  },
  spacing,
  typography: {
    useNextVariants: true,
  },
}
// tslint:enable object-literal-sort-keys

Functions

import getContrastText, {
  decomposeColor,
  convertHexToRGB,
  getContrastRatio,
  getLuminance,
} from '@tgrx/getcontrasttext'
  • default getContrastText()
getContrastText({
  background,
  contrastThreshold,
  dark,
  light,
  warnings = true
}: {
  background: string,
  contrastThreshold: number,
  dark: {text: {primary: string}},
  light: {text: {primary: string}},
  warnings?: boolean
}) 
  • decomposeColor()
decomposeColor(color: string): {type: string, values: number[]} {
  • convertHexToRGB()
convertHexToRGB(color: string)
  • getContrastRatio()
getContrastRatio(foreground: string, background: string)
  • getLuminance()
getLuminance(color: string)

Contributing

Docs

Commands - READ THIS

* nps                   # Executes the module, watching for rebuilds.
* nps help              # Displays all available commands
* nps commit            # Creates a commit, don't use `git commit -m ...`
* nps build             # Builds the module
* nps lint              # Lint checks the module
* nps test              # Test checks the module
* nps relase            # Semver actions
* nps publish           # NPM actions

FAQs

Package last updated on 19 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc