1

I get color data from firebase like this

"Color": "#E5E1FF"

and I use color code for style like this

style={[styles.addItem, { backgroundColor: item.color }]}

in this case, how can I adjust background transparent?

nura
  • 21
  • 2
  • 1
    Transparency can be controlled by 7th & 8th digit in hex color code. E.g. half transparency of your color is "#E5E1FF**80**". You may find the details in other post [https://stackoverflow.com/a/23201304/20002061](https://stackoverflow.com/a/23201304/20002061). – kiuQ Apr 03 '23 at 01:02

1 Answers1

1

Convert hex to rgba (Convert Hex to RGBA)

My example:

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';

// You can import from local files
import AssetExample from './components/AssetExample';

// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
function hexToRgbA(hex, alpha = 1) {
  var c;
  if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
    c = hex.substring(1).split('');
    if (c.length == 3) {
      c = [c[0], c[0], c[1], c[1], c[2], c[2]];
    }
    c = '0x' + c.join('');
    return (
      'rgba(' +
      [(c >> 16) & 255, (c >> 8) & 255, c & 255].join(',') +
      ',' +
      alpha +
      ')'
    );
  }
  throw new Error('Bad Hex');
}

export default function App() {
  return (
    <View style={styles.container}>
      <Text style={styles.paragraph}>
        Change code in the editor and watch it change on your phone! Save to get
        a shareable url.
      </Text>
      <Card style={{backgroundColor: hexToRgbA('#ff0000',0.5)}}> {/*second parameter of hexToRgbA define the transparency*/}
        <AssetExample />
      </Card>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  paragraph: {
    margin: 24,
    fontSize: 18,
    fontWeight: 'bold',
    textAlign: 'center',
  },
});

Michael Bahl
  • 2,941
  • 1
  • 13
  • 16