Skip to content

Commit 3767822

Browse files
committed
refactor
1 parent 4e9f8ea commit 3767822

File tree

3 files changed

+87
-49
lines changed

3 files changed

+87
-49
lines changed

ARKit.js

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
import PropTypes from 'prop-types';
1616
import React, { Component } from 'react';
1717

18-
import { whiteBalanceWithTemperature } from './lib/colorUtils';
18+
import { pickColors, pickColorsFromFile } from './lib/pickColors';
1919
import generateId from './components/lib/generateId';
2020

2121
const ARKitManager = NativeModules.ARKitManager;
@@ -170,42 +170,8 @@ ARKit.exportModel = presetId => {
170170
return ARKitManager.exportModel(property).then(result => ({ ...result, id }));
171171
};
172172

173-
ARKit.pickColors = async (
174-
{
175-
whiteBalance = true,
176-
includeRawColors = false,
177-
selection = null,
178-
// color grabber options, currently undocumented
179-
range = 40,
180-
dimension = 4,
181-
flexibility = 5,
182-
} = {},
183-
) => {
184-
const colors = await ARKitManager.pickColorsRaw({
185-
selection,
186-
range,
187-
dimension,
188-
flexibility,
189-
});
190-
if (!whiteBalance) {
191-
return colors;
192-
}
193-
const lightEstimation = await ARKitManager.getCurrentLightEstimation();
194-
195-
if (!lightEstimation) {
196-
return colors;
197-
}
198-
199-
return colors.map(({ color, ...p }) => ({
200-
color: whiteBalanceWithTemperature(
201-
color,
202-
lightEstimation.ambientColorTemperature,
203-
),
204-
...p,
205-
...(includeRawColors ? { colorRaw: color } : {}),
206-
}));
207-
};
208-
173+
ARKit.pickColors = pickColors;
174+
ARKit.pickColorsFromFile = pickColorsFromFile;
209175
ARKit.propTypes = {
210176
debug: PropTypes.bool,
211177
planeDetection: PropTypes.bool,

ios/RCTARKitManager.m

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ - (NSString *)getAssetUrl:(NSString *)localID {
155155
return assetURLStr;
156156
}
157157

158-
- (void)storeImageInPhotoAlbum:(UIImage *)image reject:(RCTPromiseRejectBlock)reject resolve:(RCTPromiseResolveBlock)resolve {
158+
- (void)storeImageInPhotoAlbum:(UIImage *)image cameraProperties:(NSDictionary *) cameraProperties reject:(RCTPromiseRejectBlock)reject resolve:(RCTPromiseResolveBlock)resolve {
159159
__block PHObjectPlaceholder *placeholder;
160160

161161
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
@@ -170,7 +170,7 @@ - (void)storeImageInPhotoAlbum:(UIImage *)image reject:(RCTPromiseRejectBlock)re
170170

171171
NSString * assetURLStr = [self getAssetUrl:localID];
172172

173-
resolve(@{@"url": assetURLStr, @"width":@(image.size.width), @"height": @(image.size.height)});
173+
resolve(@{@"url": assetURLStr, @"width":@(image.size.width), @"height": @(image.size.height), @"camera":cameraProperties});
174174
}
175175
else
176176
{
@@ -180,7 +180,7 @@ - (void)storeImageInPhotoAlbum:(UIImage *)image reject:(RCTPromiseRejectBlock)re
180180
}
181181

182182

183-
- (void)storeImageInDirectory:(UIImage *)image directory:(NSString *)directory format:(NSString *)format reject:(RCTPromiseRejectBlock)reject resolve:(RCTPromiseResolveBlock)resolve {
183+
- (void)storeImageInDirectory:(UIImage *)image directory:(NSString *)directory format:(NSString *)format cameraProperties:(NSDictionary *) cameraProperties reject:(RCTPromiseRejectBlock)reject resolve:(RCTPromiseResolveBlock)resolve {
184184
NSData *data;
185185
if([format isEqualToString:@"jpg"]) {
186186
data = UIImageJPEGRepresentation(image, 0.9);
@@ -198,15 +198,15 @@ - (void)storeImageInDirectory:(UIImage *)image directory:(NSString *)directory f
198198
NSString *filePath = [directory stringByAppendingPathComponent:uniqueFileName]; //Add the file name
199199
bool success = [data writeToFile:filePath atomically:YES]; //Write the file
200200
if(success) {
201-
resolve(@{@"url": filePath, @"width":@(image.size.width), @"height": @(image.size.height)});
201+
resolve(@{@"url": filePath, @"width":@(image.size.width), @"height": @(image.size.height), @"camera":cameraProperties});
202202
} else {
203203
// TODO use NSError from writeToFile
204204
reject(@"snapshot_error", [NSString stringWithFormat:@"could not save to '%@'", filePath], nil);
205205
}
206206

207207
}
208208

209-
- (void)storeImage:(UIImage *)image options:(NSDictionary *)options reject:(RCTPromiseRejectBlock)reject resolve:(RCTPromiseResolveBlock)resolve {
209+
- (void)storeImage:(UIImage *)image options:(NSDictionary *)options reject:(RCTPromiseRejectBlock)reject resolve:(RCTPromiseResolveBlock)resolve cameraProperties:(NSDictionary *)cameraProperties {
210210
NSString * target = @"cameraRoll";
211211
NSString * format = @"png";
212212

@@ -218,7 +218,7 @@ - (void)storeImage:(UIImage *)image options:(NSDictionary *)options reject:(RCTP
218218
}
219219
if([target isEqualToString:@"cameraRoll"]) {
220220
// camera roll / photo album
221-
[self storeImageInPhotoAlbum:image reject:reject resolve:resolve];
221+
[self storeImageInPhotoAlbum:image cameraProperties:cameraProperties reject:reject resolve:resolve ];
222222
} else {
223223
NSString * dir;
224224
if([target isEqualToString:@"cache"]) {
@@ -229,15 +229,17 @@ - (void)storeImage:(UIImage *)image options:(NSDictionary *)options reject:(RCTP
229229
} else {
230230
dir = target;
231231
}
232-
[self storeImageInDirectory:image directory:dir format:format reject:reject resolve:resolve];
232+
[self storeImageInDirectory:image directory:dir format:format cameraProperties:cameraProperties reject:reject resolve:resolve ];
233233
}
234234
}
235235

236236
RCT_EXPORT_METHOD(snapshot:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
237237
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
238238
NSDictionary * selection = options[@"selection"];
239+
NSDictionary * cameraProperties = [[ARKit sharedInstance] readCamera];
239240
UIImage *image = [[ARKit sharedInstance] getSnapshot:selection];
240-
[self storeImage:image options:options reject:reject resolve:resolve];
241+
242+
[self storeImage:image options:options reject:reject resolve:resolve cameraProperties:cameraProperties ];
241243
});
242244
}
243245

@@ -247,21 +249,26 @@ - (void)storeImage:(UIImage *)image options:(NSDictionary *)options reject:(RCTP
247249
RCT_EXPORT_METHOD(snapshotCamera:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
248250
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
249251
NSDictionary * selection = options[@"selection"];
252+
NSDictionary * cameraProperties = [[ARKit sharedInstance] readCamera];
250253
UIImage *image = [[ARKit sharedInstance] getSnapshotCamera:selection];
251-
[self storeImage:image options:options reject:reject resolve:resolve];
254+
[self storeImage:image options:options reject:reject resolve:resolve cameraProperties:cameraProperties];
252255
});
253256
}
254257

255258
RCT_EXPORT_METHOD(pickColorsRaw:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
256-
257259
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
258260

259261
NSDictionary * selection = options[@"selection"];
260262
UIImage *image = [[ARKit sharedInstance] getSnapshotCamera:selection];
261263
resolve([[ColorGrabber sharedInstance] getColorsFromImage:image options:options]);
262264
});
263-
264-
265+
}
266+
267+
RCT_EXPORT_METHOD(pickColorsRawFromFile:(NSString * )filePath options:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
268+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
269+
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
270+
resolve([[ColorGrabber sharedInstance] getColorsFromImage:image options:options]);
271+
});
265272
}
266273

267274
RCT_EXPORT_METHOD(getCamera:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {

lib/pickColors.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { NativeModules } from 'react-native';
2+
3+
import { whiteBalanceWithTemperature } from './colorUtils';
4+
5+
const ARKitManager = NativeModules.ARKitManager;
6+
7+
const doWhiteBalance = async (colors, { includeRawColors }) => {
8+
const lightEstimation = await ARKitManager.getCurrentLightEstimation();
9+
10+
if (!lightEstimation) {
11+
return colors;
12+
}
13+
14+
return colors.map(({ color, ...p }) => ({
15+
color: whiteBalanceWithTemperature(
16+
color,
17+
lightEstimation.ambientColorTemperature,
18+
),
19+
...p,
20+
...(includeRawColors ? { colorRaw: color } : {}),
21+
}));
22+
};
23+
export const pickColorsFromFile = async (
24+
filePath,
25+
{
26+
whiteBalance = true,
27+
includeRawColors = false,
28+
// color grabber options, currently undocumented
29+
range = 40,
30+
dimension = 4,
31+
flexibility = 5,
32+
} = {},
33+
) => {
34+
const colors = await ARKitManager.pickColorsRawFromFile(filePath, {
35+
range,
36+
dimension,
37+
flexibility,
38+
});
39+
if (!whiteBalance) {
40+
return colors;
41+
}
42+
return doWhiteBalance(colors, { includeRawColors });
43+
};
44+
export const pickColors = async (
45+
{
46+
whiteBalance = true,
47+
includeRawColors = false,
48+
selection = null,
49+
// color grabber options, currently undocumented
50+
range = 40,
51+
dimension = 4,
52+
flexibility = 5,
53+
} = {},
54+
) => {
55+
const colors = await ARKitManager.pickColorsRaw({
56+
selection,
57+
range,
58+
dimension,
59+
flexibility,
60+
});
61+
if (!whiteBalance) {
62+
return colors;
63+
}
64+
return doWhiteBalance(colors, { includeRawColors });
65+
};

0 commit comments

Comments
 (0)