-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ai color picker api integration (#211)
* basic API integration * improved UI and add events * track exceptions
- Loading branch information
1 parent
ed7ac29
commit 7012a49
Showing
7 changed files
with
156 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,146 @@ | ||
// components/AIColorPicker.js | ||
import React, { useState } from 'react'; | ||
import { View, TextInput, StyleSheet, ActivityIndicator } from 'react-native'; | ||
import CromaButton from './CromaButton'; | ||
import { | ||
View, | ||
TextInput, | ||
StyleSheet, | ||
ActivityIndicator, | ||
TouchableOpacity, | ||
Text | ||
} from 'react-native'; | ||
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5'; | ||
import Colors from '../constants/Colors'; | ||
import { generateAIColorSuggestions } from '../network/colors'; | ||
import { pickTextColorBasedOnBgColor } from '../libs/ColorHelper'; | ||
import { logEvent, notifyMessage, sendClientError } from '../libs/Helpers'; | ||
|
||
export default function AIColorPicker({ color, setColor }) { | ||
export default function AIColorPicker({ setColor }) { | ||
const [query, setQuery] = useState(''); | ||
const [loading, setLoading] = useState(false); | ||
const [suggestions, setSuggestions] = useState([]); | ||
const [selectedColor, setSelectedColor] = useState(null); | ||
|
||
const generateColorFromQuery = async () => { | ||
setLoading(true); | ||
// TODO: Implement the AI color generation logic based on the query | ||
// For now, let's just simulate an async operation with a delay | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
const randomColor = `#${Math.floor(Math.random() * 16777215).toString(16)}`; | ||
setColor(randomColor); | ||
try { | ||
logEvent('ai_color_picker_query_submitted'); | ||
const response = await generateAIColorSuggestions(query); | ||
const colors = response.data.colors; | ||
setSuggestions(colors); | ||
} catch (error) { | ||
console.error('Error generating AI color suggestions:', error); | ||
sendClientError('ai_color_picker_query_submitted', error.message); | ||
notifyMessage('Failed to generate suggestion: ' + error.message + ', please try again.'); | ||
} | ||
setLoading(false); | ||
}; | ||
|
||
const handleColorSelect = (hex) => { | ||
logEvent('ai_color_picker_color_selected'); | ||
setColor(hex); | ||
setSelectedColor(hex); | ||
}; | ||
|
||
return ( | ||
<View> | ||
<TextInput | ||
style={styles.textInput} | ||
placeholder="Enter a query" | ||
value={query} | ||
onChangeText={setQuery} | ||
/> | ||
<CromaButton onPress={generateColorFromQuery} disabled={loading}> | ||
{loading ? <ActivityIndicator size="small" color="#ffffff" /> : 'Generate Color'} | ||
</CromaButton> | ||
<View style={styles.inputContainer}> | ||
<TextInput | ||
style={styles.textInput} | ||
placeholder="e.g., 'Vibrant sunset by the beach'" | ||
value={query} | ||
onChangeText={setQuery} | ||
/> | ||
<TouchableOpacity | ||
style={styles.generateButton} | ||
onPress={generateColorFromQuery} | ||
disabled={loading}> | ||
{loading ? ( | ||
<ActivityIndicator size="small" color={Colors.fabPrimary} /> | ||
) : ( | ||
<FontAwesome5 name="magic" size={24} color={Colors.fabPrimary} /> | ||
)} | ||
</TouchableOpacity> | ||
</View> | ||
<View style={styles.suggestionsContainer}> | ||
{suggestions.length > 0 ? ( | ||
suggestions.map(({ name, hex }) => ( | ||
<TouchableOpacity | ||
key={hex} | ||
style={[ | ||
styles.colorPreview, | ||
{ backgroundColor: hex }, | ||
selectedColor === hex && styles.selectedColor | ||
]} | ||
onPress={() => handleColorSelect(hex)}> | ||
<Text style={[styles.colorName, { color: pickTextColorBasedOnBgColor(hex) }]}> | ||
{name} | ||
</Text> | ||
<Text style={[styles.colorHex, { color: pickTextColorBasedOnBgColor(hex) }]}> | ||
{hex} | ||
</Text> | ||
</TouchableOpacity> | ||
)) | ||
) : ( | ||
<Text style={styles.placeholderText}>Your AI-generated colors will appear here</Text> | ||
)} | ||
</View> | ||
</View> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
inputContainer: { | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
marginBottom: 16 | ||
}, | ||
textInput: { | ||
flex: 1, | ||
height: 40, | ||
borderWidth: 1, | ||
borderColor: 'gray', | ||
borderRadius: 8, | ||
paddingHorizontal: 8, | ||
marginRight: 8 | ||
}, | ||
generateButton: { | ||
borderWidth: 1, | ||
marginBottom: 16, | ||
paddingHorizontal: 8 | ||
borderColor: Colors.fabPrimary, | ||
borderRadius: 8, | ||
padding: 8 | ||
}, | ||
suggestionsContainer: { | ||
flexDirection: 'row', | ||
flexWrap: 'wrap', | ||
minHeight: 100, | ||
alignItems: 'center', | ||
justifyContent: 'center' | ||
}, | ||
colorPreview: { | ||
width: '100%', | ||
height: 100, | ||
marginTop: 16 | ||
width: 80, | ||
height: 80, | ||
borderRadius: 10, | ||
margin: 5, | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
backgroundColor: 'white', | ||
borderWidth: 1, | ||
borderColor: 'gray' | ||
}, | ||
selectedColor: { | ||
borderWidth: 2, | ||
borderColor: Colors.fabPrimary | ||
}, | ||
colorName: { | ||
fontSize: 12, | ||
fontWeight: 'bold', | ||
marginBottom: 4 | ||
}, | ||
colorHex: { | ||
fontSize: 10 | ||
}, | ||
placeholderText: { | ||
color: 'gray', | ||
fontSize: 16 | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// https://stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color | ||
export function pickTextColorBasedOnBgColor(bgColor) { | ||
var color = bgColor.charAt(0) === '#' ? bgColor.substring(1, 7) : bgColor; | ||
var r = parseInt(color.substring(0, 2), 16); // hexToR | ||
var g = parseInt(color.substring(2, 4), 16); // hexToG | ||
var b = parseInt(color.substring(4, 6), 16); // hexToB | ||
var uicolors = [r / 255, g / 255, b / 255]; | ||
var c = uicolors.map((col) => { | ||
if (col <= 0.03928) { | ||
return col / 12.92; | ||
} | ||
return Math.pow((col + 0.055) / 1.055, 2.4); | ||
}); | ||
var L = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]; | ||
return L > 0.179 ? '#000000' : '#FFFFFF'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import getAxiosClient from './axios.client'; | ||
|
||
export const generateAIColorSuggestions = async (query) => { | ||
const axiosClient = await getAxiosClient(); | ||
return axiosClient.get('colors/generate_suggestions?query=' + query); | ||
}; |