Skip to content

Commit e1c61c5

Browse files
committed
#93 colors usage added in readme
1 parent e6c4fd8 commit e1c61c5

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This is a Kotlin MultiPlatform library that provides access to the resources on
1717

1818
## Features
1919
- **Strings, Plurals, Images, Fonts, Files** to access the corresponding resources from common code;
20+
- **Colors** with light/dark mode support;
2021
- **StringDesc** for lifecycle-aware access to resources and unified localization on both platforms;
2122
- **FatFrameworkWithResourcesTask** Gradle task.
2223

@@ -375,6 +376,65 @@ We got autogenerated `MR.fonts.Raleway.italic`, `MR.fonts.Raleway.regular`, `MR.
375376
- Android: `textView.typeface = font.getTypeface(context = this)`
376377
- iOS: `textView.font = font.uiFont(withSize: 14.0)`
377378

379+
### Example 9 - pass colors
380+
Colors resources directory is `commonMain/resources/MR/colors`.
381+
Colors files is `xml` with format:
382+
```xml
383+
<?xml version="1.0" encoding="utf-8"?>
384+
<resources>
385+
<color name="valueColor">#B02743FF</color>
386+
<color name="referenceColor">@color/valueColor</color>
387+
<color name="themedColor">
388+
<light>0xB92743FF</light>
389+
<dark>7CCFEEFF</dark>
390+
</color>
391+
<color name="themedReferenceColor">
392+
<light>@color/valueColor</light>
393+
<dark>@color/referenceColor</dark>
394+
</color>
395+
</resources>
396+
```
397+
If you want use one color without light/dark theme selection:
398+
```xml
399+
<color name="valueColor">#B02743FF</color>
400+
```
401+
If you want use value of other color - use references:
402+
```xml
403+
<color name="referenceColor">@color/valueColor</color>
404+
```
405+
If you want different colors in light/dark themes:
406+
```xml
407+
<color name="themedColor">
408+
<light>0xB92743FF</light>
409+
<dark>7CCFEEFF</dark>
410+
</color>
411+
```
412+
Also themed colors can be referenced too:
413+
```xml
414+
<color name="themedReferenceColor">
415+
<light>@color/valueColor</light>
416+
<dark>@color/referenceColor</dark>
417+
</color>
418+
```
419+
420+
Colors available in common code insode `MR.colors.**` as `ColorResource`.
421+
`ColorResource` can be `ColorResource.Single` - simple color without theme selection.
422+
And can be `ColorResource.Themed` with colors for each mode.
423+
424+
You can read colors value from common code:
425+
```kotlin
426+
val color: Color = MR.colors.valueColor.color
427+
```
428+
but if you use `ColorResource.Themed` you can get current theme color only from platfrom side.
429+
Android:
430+
```kotlin
431+
val color: Color = MR.colors.valueColor.getColor(context = this)
432+
```
433+
iOS:
434+
```swift
435+
val color: UIColor = MR.colors.valueColor.getColor(UIScreen.main.traitCollection.userInterfaceStyle)
436+
```
437+
378438
### Gradle task for creating Fat Framework with resources
379439

380440
If you want to create Fat Framework for iOS with all resources from KMP Gradle module you should use

0 commit comments

Comments
 (0)