Skip to main content
Version: 7.2.1

Color

API Reference: UI.Color

Color is used to color UI objects and its elements. A Color instance is created by passing RGB-ARGB values or a hexadecimal string. There are constant and predefined colors as well.

note

The components in the example are added from the code for better showcase purposes. To learn more about the subject you can refer to:

Adding Component From Code

As a best practice, Smartface recommends using the WYSIWYG editor in order to add components and styles to your page or library. To learn how to use UI Editor better, please refer to this documentation

UI Editor Basics
import PageSampleDesign from 'generated/pages/pageSample';
import Application from '@smartface/native/application';
import View from '@smartface/native/ui/view';
import { Route, Router } from '@smartface/router';
import { withDismissAndBackButton } from '@smartface/mixins';
import { styleableComponentMixin } from '@smartface/styling-context';

class StyleableView extends styleableComponentMixin(View) {}

export default class ColorSample extends withDismissAndBackButton(PageSampleDesign) {
myView: StyleableView;
constructor(private router?: Router, private route?: Route) {
super({});
}

// The page design has been made from the code for better
// showcase purposes. As a best practice, remove this and
// use WYSIWYG editor to style your pages.
centerizeTheChildrenLayout() {
this.dispatch({
type: "updateUserStyle",
userStyle: {
flexProps: {
flexDirection: 'ROW',
justifyContent: 'CENTER',
alignItems: 'CENTER'
}
}
})
}

onShow() {
super.onShow();
Application.statusBar.visible = false;
this.headerBar.visible = false;
}

onLoad() {
super.onLoad();
this.centerizeTheChildrenLayout();

let myRedColor = 'rgb(255, 0, 0)';
let myBlueColorWithAlpha = 'rgba(100, 0, 0, 255)';
let myHEXColor = '#00A1F1';
this.myView = new StyleableView();

this.addChild(this.myView, 'myView', '.sf-view', {
width: 100,
height: 100,
backgroundColor: myHEXColor
});
}
}

Get Color from variables

info

In best practice, you should use predefined colors. Variables are defined in the variables.json file, variables.json file is located in the themes/yourTheme folder. It comes with the theme.

themes/yourTheme/variables.json
{ 
"main": "#53B175",
"backgroundMain": "rgba(245,249,254,1.0)",
"backgroundSecondary": "rgba(237,243,254,1.0)",
"textDark": "#181725",
"textLight": "#7C7C7C",
}
script/pages/page1.ts

import Page1Design from 'generated/pages/page1';
import { Route, Router } from '@smartface/router';
import { withDismissAndBackButton } from '@smartface/mixins';
import { themeService } from 'theme';

const textLight = themeService.getNativeStyle('.flMyComponent').textColor;

export default class Page1 extends withDismissAndBackButton(Page1Design) {
private disposeables: (() => void)[] = [];
constructor(private router?: Router, private route?: Route) {
super({});
}

onShow() {
super.onShow();
this.lbl.textColor = textLight;
this.lbl.text = 'Light color comes from class, and it is defined in the variables.json file';
}
onLoad() {
super.onLoad();
}
}