|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @flow strict-local |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +import type {ColorValue} from './StyleSheetTypes'; |
| 14 | +import type {ProcessedColorValue} from './processColor'; |
| 15 | + |
| 16 | +export opaque type NativeColorValue = { |
| 17 | + semantic?: Array<string>, |
| 18 | + dynamic?: { |
| 19 | + light: ?(ColorValue | ProcessedColorValue), |
| 20 | + dark: ?(ColorValue | ProcessedColorValue), |
| 21 | + }, |
| 22 | +}; |
| 23 | + |
| 24 | +export const PlatformColor = (...names: Array<string>): ColorValue => { |
| 25 | + return {semantic: names}; |
| 26 | +}; |
| 27 | + |
| 28 | +export type DynamicColorIOSTuplePrivate = { |
| 29 | + light: ColorValue, |
| 30 | + dark: ColorValue, |
| 31 | +}; |
| 32 | + |
| 33 | +export const DynamicColorIOSPrivate = ( |
| 34 | + tuple: DynamicColorIOSTuplePrivate, |
| 35 | +): ColorValue => { |
| 36 | + return {dynamic: {light: tuple.light, dark: tuple.dark}}; |
| 37 | +}; |
| 38 | + |
| 39 | +export const normalizeColorObject = ( |
| 40 | + color: NativeColorValue, |
| 41 | +): ?ProcessedColorValue => { |
| 42 | + if ('semantic' in color) { |
| 43 | + // an ios semantic color |
| 44 | + return color; |
| 45 | + } else if ('dynamic' in color && color.dynamic !== undefined) { |
| 46 | + const normalizeColor = require('./normalizeColor'); |
| 47 | + |
| 48 | + // a dynamic, appearance aware color |
| 49 | + const dynamic = color.dynamic; |
| 50 | + const dynamicColor: NativeColorValue = { |
| 51 | + dynamic: { |
| 52 | + light: normalizeColor(dynamic.light), |
| 53 | + dark: normalizeColor(dynamic.dark), |
| 54 | + }, |
| 55 | + }; |
| 56 | + return dynamicColor; |
| 57 | + } |
| 58 | + |
| 59 | + return null; |
| 60 | +}; |
| 61 | + |
| 62 | +export const processColorObject = ( |
| 63 | + color: NativeColorValue, |
| 64 | +): ?NativeColorValue => { |
| 65 | + if ('dynamic' in color && color.dynamic != null) { |
| 66 | + const processColor = require('./processColor'); |
| 67 | + const dynamic = color.dynamic; |
| 68 | + const dynamicColor: NativeColorValue = { |
| 69 | + dynamic: { |
| 70 | + light: processColor(dynamic.light), |
| 71 | + dark: processColor(dynamic.dark), |
| 72 | + }, |
| 73 | + }; |
| 74 | + return dynamicColor; |
| 75 | + } |
| 76 | + return color; |
| 77 | +}; |
0 commit comments