Giter Site home page Giter Site logo

mrousavy / colorwaver Goto Github PK

View Code? Open in Web Editor NEW
624.0 8.0 74.0 24.25 MB

๐ŸŽจ An app to detect color palettes in the real world - powered by VisionCamera

License: Other

JavaScript 2.66% Starlark 1.33% Java 14.20% Objective-C 8.72% Ruby 4.99% Swift 5.72% TypeScript 35.08% Kotlin 22.32% Shell 4.97%
app react-native react ios android palette color colors camera visioncamera

colorwaver's Introduction

Colorwaver

An app to detect colorwaves (swatches/palettes) in the real world - powered by VisionCamera and Reanimated.

I wrote this app in less than a day, a speed simply not matched by native app development. Because it's written in React Native (TypeScript/JavaScript), it runs both on iOS and Android, but performance critical parts (e.g. the image processing algorithm or the animations) are backed by native Objective-C/Swift/Java code.

See this Tweet for more information.

Buy Me a Coffee at ko-fi.com

Try it!

Download the repository and run the following commands to try Colorwaver for yourself:

iOS

yarn try-ios

Android

yarn try-android

Project structure

This is a bare React Native project, created with create-react-native-app.


  • ๐Ÿ“ src: Contains the actual TypeScript + React (-Native) front-end for the Colorwaver App.
    • ๐Ÿ“„ src/getColorPalette.ts: Exposes the native iOS/Android frame processor plugin as a JS function with TypeScript types. This function has to be called from a frame processor. ('worklet')

    • ๐Ÿ“„ src/Router.tsx: The main component that gets registered by index.js. This acts as a main navigator, navigating either to the Splash Page (Permissions) or the App Page (Main App) depending on whether the user has granted permissions or not.

    • ๐Ÿ“„ src/Splash.tsx: The first Splash page to ask the user for Camera permission.

    • ๐Ÿ“„ src/App.tsx: Contains the actual source code for the App's front-end.

      • VisionCamera is used to get a Camera device and display a Camera component. Also, a frame processor (a function that gets called for every frame the camera "sees") gets attached, which calls the native iOS/Android frame processor plugin.
      • Reanimated is used to smoothly animate between color changes.

      Because VisionCamera also uses the Worklet API, the entire process between receiving a camera frame and actually displaying the palette's colors does not use the React-JS Thread at all. The frame processing runs on a separate Thread from VisionCamera, which then dispatches the animations on the Reanimated UI Thread. This is why the App runs as smooth as a native iOS or Android app.

    • ๐Ÿ“„ src/useAnimatedColor.ts: A helper function to animate color changes with SharedValues.


  • ๐Ÿ“ ios: Contains the basic skeleton for a React Native iOS app, plus the native getColorPalette(...) Frame Processor Plugin.
    • ๐Ÿ“„ ios/PaletteFrameProcessorPlugin.m: Declares the Swift frame processor plugin "getColorPalette(...)".

    • ๐Ÿ“„ ios/PaletteFrameProcessorPlugin.swift: Contains the actual Swift code for the native iOS frame processor plugin "getColorPalette(...)".

      This uses the CoreImage API to convert the CMSampleBuffer to a UIImage, and then uses the UIImageColors library to build the color palette.

      VisionCamera's frame processor API is used to expose this function as a frame processor plugin.

    • ๐Ÿ“„ ios/Colorwaver-Bridging-Header.h: A Bridging Header to import Objective-C headers into Swift.

    • ๐Ÿ“„ ios/Podfile: Adds the UIImageColors library.

    • ๐Ÿ“„ ios/UIColor+hexString.swift: An extension for UIColor to convert UIColor instances to strings. This is required because React Native handles colors as strings.


  • ๐Ÿ“ android: Contains the basic skeleton for a React Native Android app, plus the native getColorPalette(...) Frame Processor Plugin.
    • ๐Ÿ“„ android/app/build.gradle: The gradle build file for the Android project. The following third-party dependencies are installed:

      • androidx.camera: camera-core
      • androidx.palette: palette
    • ๐Ÿ“ android/app/src/main/java/com/colorwaver/utils: Contains two files copied from android/camera-samples to convert ImageProxy instances to Bitmaps. (YuvToRgbConverter.kt)

    • ๐Ÿ“ android/app/src/main/java/com/colorwaver: Contains the actual Java source code of the Project.

    • ๐Ÿ“„ android/app/src/main/java/com/colorwaver/MainApplication.java: Sets up react-native-reanimated.

    • ๐Ÿ“„ android/app/src/main/java/com/colorwaver/MainActivity.java: Installs the PaletteFrameProcessorPlugin frame processor plugin inside of the onCreate method.

    • ๐Ÿ“„ android/app/src/main/java/com/colorwaver/PaletteFrameProcessorPlugin.java: Contains the actual Java code for the native Android frame processor plugin "getColorPalette(...)".

      This uses the YuvToRgbConverter to convert the ImageProxy to a Bitmap, and then passes that to the Palette API from AndroidX to build the color palette.

      VisionCamera's frame processor API is used to expose this function as a frame processor plugin.


  • ๐Ÿ“„ babel.config.js: Adds the native frame processor plugin getColorPalette (called __getColorPalette) to Reanimated's global list.

Credits

colorwaver's People

Contributors

benschlegel avatar imgbot[bot] avatar mrousavy avatar simek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

colorwaver's Issues

"Attempting to assign to readonly property" when using color interpolation

My animated color hook:

export function useAnimatedColor(
  color: Reanimated.SharedValue<string>,
): Readonly<Reanimated.SharedValue<string | number>> {
  const animation = useSharedValue(0);
  const colorFrom = useSharedValue(DEFAULT_COLOR);
  const colorTo = useSharedValue(color.value);

  useAnimatedReaction(
    () => color.value,
    (newColor, prevColor) => {
      animation.value = 0;
      colorFrom.value = prevColor ?? DEFAULT_COLOR;
      colorTo.value = newColor;
      animation.value = withTiming(1, {
        duration: 150,
        easing: Easing.linear,
      });
      console.log(`Animating from ${prevColor} -> ${newColor}`);
    },
  );

  // TODO: Using colorFrom and colorTo in here raises "Attempting to assign to readonly property" error...
  return useDerivedValue(() =>
    interpolateColor(animation.value, [0, 1], [colorFrom.value, colorTo.value]),
  );
}

is causing this error:

See software-mansion/react-native-reanimated#2329 for more details.

Right now, I've hotfixed this by using a custom patch for react-native-reanimated that completely removes the RGB cache. This now runs correctly, but ideally I want to fix this and make use of the cache for performance reasons.

Errors - while running the react-native run-android

Hi @mrousavy ,

This is truly amazing work!!

I was trying the instructions to run it for my android device, on a windows machine, got the below error:

Was hoping if you too had any similar issue:

FAILURE: Build failed with an exception.

  • Where:
    Build file 'C:\Usersxxxxxvision_camera\Colorwaver\android\app\build.gradle' line: 162

  • What went wrong:
    A problem occurred evaluating project ':app'.

Could not get unknown property 'release' for SigningConfig container of type org.gradle.api.internal.FactoryNamedDomainObjectContainer.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 2s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

  • Where:
    Build file 'C:\Users\xxxxx\Colorwaver\android\app\build.gradle' line: 162

  • What went wrong:
    A problem occurred evaluating project ':app'.

Could not get unknown property 'release' for SigningConfig container of type org.gradle.api.internal.FactoryNamedDomainObjectContainer.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

Error when generating using npx react-native run-android --variant=release

I get this error when trying to compile using the command:
npx react-native run-android --variant=release

In file included from ../../../../src/main/cpp/JSIJNIConversion.cpp:25:
../../../../src/main/cpp/java-bindings/JHashMap.h:16:8: error: redefinition of 'JHashMap'
struct JHashMap : JavaClass<JHashMap<K, V>, JMap<K, V>> {
^
../../../../build/fbjni-0.3.0-headers.jar\fbjni/detail/Iterator.h:170:8: note: previous definition is here
struct JHashMap : JavaClass<JHashMap<K, V>, JMap<K, V>> {
^
1 error generated.

Gradle-build error while try-android

image
When I run react-native run-android this error pops up. My phone is connected through adb.

This is an amazing app and I would love to try it. Plz help me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.