|
1 | 1 | # API |
2 | 2 |
|
| 3 | +## `createMaterialPalette()` |
| 4 | + |
| 5 | +```js |
| 6 | +createMaterialPalette( |
| 7 | + image: Image, |
| 8 | + options?: Options, |
| 9 | + defaults?: PaletteDefaults, |
| 10 | +): Promise<PaletteInstance> |
| 11 | + |
| 12 | +// Number is the opaque type returned by require('./image.jpg') |
| 13 | +type Image = number | { uri: string } |
| 14 | + |
| 15 | +type Options = { |
| 16 | + region?: { top: number, left: number, bottom: number, right: number }, |
| 17 | + maximumColorCount?: number = 16, |
| 18 | + type?: ColorProfile | Array<ColorProfile> = 'vibrant', |
| 19 | +} |
| 20 | + |
| 21 | +type PaletteDefaults = { |
| 22 | + [key: ColorProfile]: DefaultSwatch, |
| 23 | +}; |
| 24 | + |
| 25 | +type PaletteInstance = { |
| 26 | + [key: ColorProfile]: ?Swatch, |
| 27 | +}; |
| 28 | + |
| 29 | +type Swatch = { |
| 30 | + population: number, // number of pixels |
| 31 | + color: string, // color for swatch, |
| 32 | + bodyTextColor: string, // appropriate color to use for any 'body' text |
| 33 | + titleTextColor: string, // appropriate color to use for any 'title' text |
| 34 | +} |
| 35 | + |
| 36 | +type ColorProfile = |
| 37 | + | 'muted' |
| 38 | + | 'vibrant' |
| 39 | + | 'darkMuted' |
| 40 | + | 'darkVibrant' |
| 41 | + | 'lightMuted' |
| 42 | + | 'lightVibrant'; |
| 43 | + |
| 44 | +type DefaultSwatch = { |
| 45 | + color: string, |
| 46 | + bodyTextColor: string, |
| 47 | + titleTextColor: string, |
| 48 | +}; |
| 49 | + |
| 50 | +``` |
| 51 | +
|
| 52 | +### Description |
| 53 | +`createMaterialPalette` is a function that returns a Promise that when resolved, will provide a palette instance with color information about the image provided. |
| 54 | +
|
| 55 | +### Arguments |
| 56 | +* `image: Image` (__required__) - Local image to create palette from (`require('path/to/image')`) or object with remote URI adress from which the image can be downloaded (`{ uri: 'http://some-domain.ext/image.png' }`). |
| 57 | +
|
| 58 | +* `options?: Options` (optional) - Options for palette creation. |
| 59 | + * `region` - Indicates what area of the bitmap the builder uses when creating the palette. |
| 60 | + * `maximumColorCount` - Sets the maximum number of colors in your palette. The default value is 16, and the optimal value depends on the source image. For landscapes, optimal values range from 8-16 while pictures with faces usually have values that fall between 24-32. |
| 61 | + * `type` - Color profiles we aim to target. Defaults to `vibrant` |
| 62 | + |
| 63 | +
|
| 64 | +* `defaults?: PaletteDefaults` (optional) - Defaults which will be used, if the specific color profile is not found. |
| 65 | +
|
| 66 | +### Examples |
| 67 | +
|
| 68 | +#### Creating a palette from a network resource, with 'vibrant' color profile, maximumColorCount = 16 and the whole region of the image (default behaviour) |
| 69 | +```js |
| 70 | +import { createMaterialPalette } from "react-native-material-palette"; |
| 71 | + |
| 72 | +const palette = await createMaterialPalette({ uri: 'http://dummySite/images/yummy.jpg' }); |
| 73 | +``` |
| 74 | +
|
| 75 | +#### Creating a palette from an internal image asset, with 'muted' and 'lightVibrant' color profiles, maximumColorCount = 32 and a specific region of the image |
| 76 | +```js |
| 77 | +import { createMaterialPalette } from "react-native-material-palette"; |
| 78 | + |
| 79 | +const palette = await createMaterialPalette(require('./assets/image.jpg'), { |
| 80 | + region: { top: 0, left: 0, bottom: 50, right: 50}, |
| 81 | + maximumColorCount: 32, |
| 82 | + type: ['muted', 'lightVibrant'], |
| 83 | +}); |
| 84 | +``` |
| 85 | +
|
| 86 | +#### Creating a palette from an internal image asset and custom defaults |
| 87 | +```js |
| 88 | +import { createMaterialPalette } from "react-native-material-palette"; |
| 89 | + |
| 90 | +const palette = await createMaterialPalette( |
| 91 | + require('./assets/image.jpg'), |
| 92 | + { |
| 93 | + type: ['lightVibrant', 'darkMuted'], |
| 94 | + }, |
| 95 | + { |
| 96 | + darkMuted: { |
| 97 | + color: '#000000', |
| 98 | + bodyTextColor: '#B2B2B2', |
| 99 | + titleTextColor: '#F4F4F4', |
| 100 | + }, |
| 101 | + }, |
| 102 | +); |
| 103 | +``` |
| 104 | +
|
3 | 105 | ## `MaterialPaletteProvider` |
4 | 106 |
|
5 | 107 | ### Example of usage: |
@@ -34,39 +136,14 @@ class App extends React.Component { |
34 | 136 | The concept is very similar to `Provider` component from `react-redux`. |
35 | 137 |
|
36 | 138 | ### Props |
37 | | -* `image: Image` (__required__) - Local image to create palette from (`require('path/to/image')`) or object with remote URI adress from which the image can be downloaded (`{ uri: 'http://some-domain.ext/image.png' }`) - same as `image` in `MaterialPalette.create` function. |
| 139 | +* `image: Image` (__required__) - same as `image` in `createMaterialPalette` function. |
38 | 140 |
|
39 | | -* `options?: Options` (optional) - Options for palette creation - same as `options` in `MaterialPalette.create` function: |
40 | | - ```javascript |
41 | | - type Options = { |
42 | | - region?: { top: number, left: number, bottom: number, right: number }, |
43 | | - maximumColorCount?: number = 16, |
44 | | - type?: ColorProfile | Array<ColorProfile> = 'vibrant', |
45 | | - } |
46 | | - ``` |
| 141 | +* `options?: Options` (optional) - same as `options` in `createMaterialPalette` function. |
47 | 142 |
|
48 | | -* `defaults?: PaletteDefaults` (optional) - Global defaults which will be propagated to each _connected_ component, alongside with palette instance, which will be used, if the specific color profile is not found: |
49 | | - ```javascript |
50 | | - type ColorProfile = |
51 | | - | 'muted' |
52 | | - | 'vibrant' |
53 | | - | 'darkMuted' |
54 | | - | 'darkVibrant' |
55 | | - | 'lightMuted' |
56 | | - | 'lightVibrant'; |
57 | | - |
58 | | - type DefaultSwatch = { |
59 | | - color: string, |
60 | | - bodyTextColor: string, |
61 | | - titleTextColor: string, |
62 | | - }; |
63 | | - |
64 | | - type PaletteDefaults = { |
65 | | - [key: ColorProfile]: DefaultSwatch, |
66 | | - }; |
67 | | - ``` |
| 143 | +* `defaults?: PaletteDefaults` (optional) - Global defaults which will be propagated to each _connected_ component, alongside with palette instance, which will be used, if the specific color profile is not found. |
68 | 144 |
|
69 | 145 | * `forceRender?: boolean` (optional) - Forces to render the children regardless whether the palette is being created. __Does not take effect if `LoaderComponent` is specified!__ |
| 146 | +
|
70 | 147 | * `LoaderComponent: React$Component<*, *, *> | ((...args: *) => React$Element<*>)` (optional) - If specified, will render the passed component while waiting for palette to be created: |
71 | 148 | * `<MateriaPaletteProvider LoaderComponent={SpinnerComponent}>` - will render `SpinnerComponent`, |
72 | 149 | * `<MateriaPaletteProvider LoaderComponent={() => <Text>Loading</Text>)}>` - will render `Text` component with _Loading_. |
@@ -96,7 +173,7 @@ export default withMaterialPalette( |
96 | 173 | ``` |
97 | 174 |
|
98 | 175 | ### Description |
99 | | -`withMaterialPalette` is a function that returns a Higher Order Component (HOC), which allows to seemlessy _connect_ to the `MaterialPaletteProvider` and get the palette instance via context. |
| 176 | +`withMaterialPalette` is a function that returns a Higher Order Component (HOC), which allows to seamlessly _connect_ to the `MaterialPaletteProvider` and get the palette instance via context. |
100 | 177 |
|
101 | 178 | Under the hood, it is a function factory (it returns a new function), similarily to `connect` from `react-redux`, to allow to be used as a decorator: |
102 | 179 | ```javascript |
|
0 commit comments